How can I convert a NameValueCollection to a KeyValuePair












0















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);
}









share|improve this question























  • Keep in mind that name value collection - like http headers allows single key with multiple values.

    – Ondrej Svejdar
    Nov 20 '18 at 22:28
















0















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);
}









share|improve this question























  • Keep in mind that name value collection - like http headers allows single key with multiple values.

    – Ondrej Svejdar
    Nov 20 '18 at 22:28














0












0








0








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);
}









share|improve this question














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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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



















  • 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












3 Answers
3






active

oldest

votes


















0














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));
}





share|improve this answer


























  • 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



















0














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));
}





share|improve this answer
























  • 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



















0














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());
}





share|improve this answer























    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
    });


    }
    });














    draft saved

    draft discarded


















    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









    0














    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));
    }





    share|improve this answer


























    • 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
















    0














    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));
    }





    share|improve this answer


























    • 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














    0












    0








    0







    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));
    }





    share|improve this answer















    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));
    }






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 29 '18 at 1:08

























    answered Nov 20 '18 at 22:24









    pamcevoypamcevoy

    455411




    455411













    • 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



















    • 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

















    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













    0














    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));
    }





    share|improve this answer
























    • 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
















    0














    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));
    }





    share|improve this answer
























    • 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














    0












    0








    0







    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));
    }





    share|improve this answer













    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));
    }






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 20 '18 at 22:27









    Olivier Jacot-DescombesOlivier Jacot-Descombes

    68.3k890140




    68.3k890140













    • 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



















    • 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

















    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











    0














    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());
    }





    share|improve this answer




























      0














      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());
      }





      share|improve this answer


























        0












        0








        0







        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());
        }





        share|improve this answer













        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());
        }






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 21 '18 at 0:25









        John WuJohn Wu

        30.8k42752




        30.8k42752






























            draft saved

            draft discarded




















































            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.




            draft saved


            draft discarded














            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





















































            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







            這個網誌中的熱門文章

            Hercules Kyvelos

            Tangent Lines Diagram Along Smooth Curve

            Yusuf al-Mu'taman ibn Hud