How can I convert a NameValueCollection to a KeyValuePair
I want to convert a NameValueCollection to a KeyValuePair. Is there a way to do this easily for just a single value in a NameValueCollection?
I have this right now but it seems kind of verbose:
private KeyValuePair<string, string> GetEtagHeader(NameValueCollection collection)
{
var etagValue = collection.Get(HttpRequestHeader.IfMatch.ToString());
return new KeyValuePair<string, string>(HttpRequestHeader.IfMatch.ToString(), etagValue);
}
c# keyvaluepair namevaluecollection
add a comment |
I want to convert a NameValueCollection to a KeyValuePair. Is there a way to do this easily for just a single value in a NameValueCollection?
I have this right now but it seems kind of verbose:
private KeyValuePair<string, string> GetEtagHeader(NameValueCollection collection)
{
var etagValue = collection.Get(HttpRequestHeader.IfMatch.ToString());
return new KeyValuePair<string, string>(HttpRequestHeader.IfMatch.ToString(), etagValue);
}
c# keyvaluepair namevaluecollection
Keep in mind that name value collection - like http headers allows single key with multiple values.
– Ondrej Svejdar
Nov 20 '18 at 22:28
add a comment |
I want to convert a NameValueCollection to a KeyValuePair. Is there a way to do this easily for just a single value in a NameValueCollection?
I have this right now but it seems kind of verbose:
private KeyValuePair<string, string> GetEtagHeader(NameValueCollection collection)
{
var etagValue = collection.Get(HttpRequestHeader.IfMatch.ToString());
return new KeyValuePair<string, string>(HttpRequestHeader.IfMatch.ToString(), etagValue);
}
c# keyvaluepair namevaluecollection
I want to convert a NameValueCollection to a KeyValuePair. Is there a way to do this easily for just a single value in a NameValueCollection?
I have this right now but it seems kind of verbose:
private KeyValuePair<string, string> GetEtagHeader(NameValueCollection collection)
{
var etagValue = collection.Get(HttpRequestHeader.IfMatch.ToString());
return new KeyValuePair<string, string>(HttpRequestHeader.IfMatch.ToString(), etagValue);
}
c# keyvaluepair namevaluecollection
c# keyvaluepair namevaluecollection
asked Nov 20 '18 at 22:08
R DoolabhR Doolabh
969143158
969143158
Keep in mind that name value collection - like http headers allows single key with multiple values.
– Ondrej Svejdar
Nov 20 '18 at 22:28
add a comment |
Keep in mind that name value collection - like http headers allows single key with multiple values.
– Ondrej Svejdar
Nov 20 '18 at 22:28
Keep in mind that name value collection - like http headers allows single key with multiple values.
– Ondrej Svejdar
Nov 20 '18 at 22:28
Keep in mind that name value collection - like http headers allows single key with multiple values.
– Ondrej Svejdar
Nov 20 '18 at 22:28
add a comment |
3 Answers
3
active
oldest
votes
I'm not sure how much shorter you can get it.
One possibility is to put the Get in where you create the KeyValuePair
private static KeyValuePair<string, string> GetEtagHeader(NameValueCollection collection)
{
string key = HttpRequestHeader.IfMatch.ToString();
return new KeyValuePair(key, collection.Get(key));
}
That should serve your case. I'd go a step further and split it into 2 methods - one for your specific case and one generic helper.
private static KeyValuePair<string, string> GetEtagHeader(NameValueCollection collection)
{
return ToKeyValuePair(HttpRequestHeader.IfMatch.ToString(), collection);
}
private static KeyValuePair<string, string> ToKeyValuePair(string key, NameValueCollection collection)
{
return new KeyValuePair(key, collection.Get(key));
}
These methods should bestatic
since they don't reference any members of the enclosing class. Better yet, they could be extension methods with athis NameValueCollection collection
parameter.
– TypeIA
Nov 20 '18 at 22:44
good ideas, thanks
– pamcevoy
Nov 23 '18 at 13:34
add a comment |
It would be less verbose if you put HttpRequestHeader.IfMatch.ToString()
into a temp variable and instead inline the temp etagValue
:
private KeyValuePair<string, string> GetEtagHeader(NameValueCollection collection)
{
string key = HttpRequestHeader.IfMatch.ToString();
return new KeyValuePair<string, string>(key, collection.Get(key));
}
The method should bestatic
since it doesn't reference any members of the enclosing class. Better yet, it could be an extension method with athis NameValueCollection collection
parameter.
– TypeIA
Nov 20 '18 at 22:44
Yes, unlessHttpRequestHeader
is an instance property.
– Olivier Jacot-Descombes
Nov 20 '18 at 22:48
add a comment |
If it were me, I'd define an extension method like this one:
public static class ExtensionMethods
{
static public KeyValuePair<string,string> GetPair(this NameValueCollection source, string key)
{
return new KeyValuePair<string, string>
(
key,
source.Get(key)
);
}
}
Then you can just write your original code like this:
private KeyValuePair<string, string> GetEtagHeader(NameValueCollection collection)
{
return collection.GetPair(HttpRequestHeader.IfMatch.ToString());
}
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53402339%2fhow-can-i-convert-a-namevaluecollection-to-a-keyvaluepair%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
I'm not sure how much shorter you can get it.
One possibility is to put the Get in where you create the KeyValuePair
private static KeyValuePair<string, string> GetEtagHeader(NameValueCollection collection)
{
string key = HttpRequestHeader.IfMatch.ToString();
return new KeyValuePair(key, collection.Get(key));
}
That should serve your case. I'd go a step further and split it into 2 methods - one for your specific case and one generic helper.
private static KeyValuePair<string, string> GetEtagHeader(NameValueCollection collection)
{
return ToKeyValuePair(HttpRequestHeader.IfMatch.ToString(), collection);
}
private static KeyValuePair<string, string> ToKeyValuePair(string key, NameValueCollection collection)
{
return new KeyValuePair(key, collection.Get(key));
}
These methods should bestatic
since they don't reference any members of the enclosing class. Better yet, they could be extension methods with athis NameValueCollection collection
parameter.
– TypeIA
Nov 20 '18 at 22:44
good ideas, thanks
– pamcevoy
Nov 23 '18 at 13:34
add a comment |
I'm not sure how much shorter you can get it.
One possibility is to put the Get in where you create the KeyValuePair
private static KeyValuePair<string, string> GetEtagHeader(NameValueCollection collection)
{
string key = HttpRequestHeader.IfMatch.ToString();
return new KeyValuePair(key, collection.Get(key));
}
That should serve your case. I'd go a step further and split it into 2 methods - one for your specific case and one generic helper.
private static KeyValuePair<string, string> GetEtagHeader(NameValueCollection collection)
{
return ToKeyValuePair(HttpRequestHeader.IfMatch.ToString(), collection);
}
private static KeyValuePair<string, string> ToKeyValuePair(string key, NameValueCollection collection)
{
return new KeyValuePair(key, collection.Get(key));
}
These methods should bestatic
since they don't reference any members of the enclosing class. Better yet, they could be extension methods with athis NameValueCollection collection
parameter.
– TypeIA
Nov 20 '18 at 22:44
good ideas, thanks
– pamcevoy
Nov 23 '18 at 13:34
add a comment |
I'm not sure how much shorter you can get it.
One possibility is to put the Get in where you create the KeyValuePair
private static KeyValuePair<string, string> GetEtagHeader(NameValueCollection collection)
{
string key = HttpRequestHeader.IfMatch.ToString();
return new KeyValuePair(key, collection.Get(key));
}
That should serve your case. I'd go a step further and split it into 2 methods - one for your specific case and one generic helper.
private static KeyValuePair<string, string> GetEtagHeader(NameValueCollection collection)
{
return ToKeyValuePair(HttpRequestHeader.IfMatch.ToString(), collection);
}
private static KeyValuePair<string, string> ToKeyValuePair(string key, NameValueCollection collection)
{
return new KeyValuePair(key, collection.Get(key));
}
I'm not sure how much shorter you can get it.
One possibility is to put the Get in where you create the KeyValuePair
private static KeyValuePair<string, string> GetEtagHeader(NameValueCollection collection)
{
string key = HttpRequestHeader.IfMatch.ToString();
return new KeyValuePair(key, collection.Get(key));
}
That should serve your case. I'd go a step further and split it into 2 methods - one for your specific case and one generic helper.
private static KeyValuePair<string, string> GetEtagHeader(NameValueCollection collection)
{
return ToKeyValuePair(HttpRequestHeader.IfMatch.ToString(), collection);
}
private static KeyValuePair<string, string> ToKeyValuePair(string key, NameValueCollection collection)
{
return new KeyValuePair(key, collection.Get(key));
}
edited Nov 29 '18 at 1:08
answered Nov 20 '18 at 22:24
pamcevoypamcevoy
455411
455411
These methods should bestatic
since they don't reference any members of the enclosing class. Better yet, they could be extension methods with athis NameValueCollection collection
parameter.
– TypeIA
Nov 20 '18 at 22:44
good ideas, thanks
– pamcevoy
Nov 23 '18 at 13:34
add a comment |
These methods should bestatic
since they don't reference any members of the enclosing class. Better yet, they could be extension methods with athis NameValueCollection collection
parameter.
– TypeIA
Nov 20 '18 at 22:44
good ideas, thanks
– pamcevoy
Nov 23 '18 at 13:34
These methods should be
static
since they don't reference any members of the enclosing class. Better yet, they could be extension methods with a this NameValueCollection collection
parameter.– TypeIA
Nov 20 '18 at 22:44
These methods should be
static
since they don't reference any members of the enclosing class. Better yet, they could be extension methods with a this NameValueCollection collection
parameter.– TypeIA
Nov 20 '18 at 22:44
good ideas, thanks
– pamcevoy
Nov 23 '18 at 13:34
good ideas, thanks
– pamcevoy
Nov 23 '18 at 13:34
add a comment |
It would be less verbose if you put HttpRequestHeader.IfMatch.ToString()
into a temp variable and instead inline the temp etagValue
:
private KeyValuePair<string, string> GetEtagHeader(NameValueCollection collection)
{
string key = HttpRequestHeader.IfMatch.ToString();
return new KeyValuePair<string, string>(key, collection.Get(key));
}
The method should bestatic
since it doesn't reference any members of the enclosing class. Better yet, it could be an extension method with athis NameValueCollection collection
parameter.
– TypeIA
Nov 20 '18 at 22:44
Yes, unlessHttpRequestHeader
is an instance property.
– Olivier Jacot-Descombes
Nov 20 '18 at 22:48
add a comment |
It would be less verbose if you put HttpRequestHeader.IfMatch.ToString()
into a temp variable and instead inline the temp etagValue
:
private KeyValuePair<string, string> GetEtagHeader(NameValueCollection collection)
{
string key = HttpRequestHeader.IfMatch.ToString();
return new KeyValuePair<string, string>(key, collection.Get(key));
}
The method should bestatic
since it doesn't reference any members of the enclosing class. Better yet, it could be an extension method with athis NameValueCollection collection
parameter.
– TypeIA
Nov 20 '18 at 22:44
Yes, unlessHttpRequestHeader
is an instance property.
– Olivier Jacot-Descombes
Nov 20 '18 at 22:48
add a comment |
It would be less verbose if you put HttpRequestHeader.IfMatch.ToString()
into a temp variable and instead inline the temp etagValue
:
private KeyValuePair<string, string> GetEtagHeader(NameValueCollection collection)
{
string key = HttpRequestHeader.IfMatch.ToString();
return new KeyValuePair<string, string>(key, collection.Get(key));
}
It would be less verbose if you put HttpRequestHeader.IfMatch.ToString()
into a temp variable and instead inline the temp etagValue
:
private KeyValuePair<string, string> GetEtagHeader(NameValueCollection collection)
{
string key = HttpRequestHeader.IfMatch.ToString();
return new KeyValuePair<string, string>(key, collection.Get(key));
}
answered Nov 20 '18 at 22:27
Olivier Jacot-DescombesOlivier Jacot-Descombes
68.3k890140
68.3k890140
The method should bestatic
since it doesn't reference any members of the enclosing class. Better yet, it could be an extension method with athis NameValueCollection collection
parameter.
– TypeIA
Nov 20 '18 at 22:44
Yes, unlessHttpRequestHeader
is an instance property.
– Olivier Jacot-Descombes
Nov 20 '18 at 22:48
add a comment |
The method should bestatic
since it doesn't reference any members of the enclosing class. Better yet, it could be an extension method with athis NameValueCollection collection
parameter.
– TypeIA
Nov 20 '18 at 22:44
Yes, unlessHttpRequestHeader
is an instance property.
– Olivier Jacot-Descombes
Nov 20 '18 at 22:48
The method should be
static
since it doesn't reference any members of the enclosing class. Better yet, it could be an extension method with a this NameValueCollection collection
parameter.– TypeIA
Nov 20 '18 at 22:44
The method should be
static
since it doesn't reference any members of the enclosing class. Better yet, it could be an extension method with a this NameValueCollection collection
parameter.– TypeIA
Nov 20 '18 at 22:44
Yes, unless
HttpRequestHeader
is an instance property.– Olivier Jacot-Descombes
Nov 20 '18 at 22:48
Yes, unless
HttpRequestHeader
is an instance property.– Olivier Jacot-Descombes
Nov 20 '18 at 22:48
add a comment |
If it were me, I'd define an extension method like this one:
public static class ExtensionMethods
{
static public KeyValuePair<string,string> GetPair(this NameValueCollection source, string key)
{
return new KeyValuePair<string, string>
(
key,
source.Get(key)
);
}
}
Then you can just write your original code like this:
private KeyValuePair<string, string> GetEtagHeader(NameValueCollection collection)
{
return collection.GetPair(HttpRequestHeader.IfMatch.ToString());
}
add a comment |
If it were me, I'd define an extension method like this one:
public static class ExtensionMethods
{
static public KeyValuePair<string,string> GetPair(this NameValueCollection source, string key)
{
return new KeyValuePair<string, string>
(
key,
source.Get(key)
);
}
}
Then you can just write your original code like this:
private KeyValuePair<string, string> GetEtagHeader(NameValueCollection collection)
{
return collection.GetPair(HttpRequestHeader.IfMatch.ToString());
}
add a comment |
If it were me, I'd define an extension method like this one:
public static class ExtensionMethods
{
static public KeyValuePair<string,string> GetPair(this NameValueCollection source, string key)
{
return new KeyValuePair<string, string>
(
key,
source.Get(key)
);
}
}
Then you can just write your original code like this:
private KeyValuePair<string, string> GetEtagHeader(NameValueCollection collection)
{
return collection.GetPair(HttpRequestHeader.IfMatch.ToString());
}
If it were me, I'd define an extension method like this one:
public static class ExtensionMethods
{
static public KeyValuePair<string,string> GetPair(this NameValueCollection source, string key)
{
return new KeyValuePair<string, string>
(
key,
source.Get(key)
);
}
}
Then you can just write your original code like this:
private KeyValuePair<string, string> GetEtagHeader(NameValueCollection collection)
{
return collection.GetPair(HttpRequestHeader.IfMatch.ToString());
}
answered Nov 21 '18 at 0:25
John WuJohn Wu
30.8k42752
30.8k42752
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53402339%2fhow-can-i-convert-a-namevaluecollection-to-a-keyvaluepair%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Keep in mind that name value collection - like http headers allows single key with multiple values.
– Ondrej Svejdar
Nov 20 '18 at 22:28