HttpValueCollection and NameValueCollection

前端 未结 2 1371
孤独总比滥情好
孤独总比滥情好 2020-12-14 15:21

What is the difference between HttpValueCollection and NameValueCollection?
If possible please explain with example.

Thanks

2条回答
  •  自闭症患者
    2020-12-14 15:56

    NameValueCollection is case sensitive for the keys, HttpValueCollection isn't. Also HttpValueCollection is an internal class that derives from NameValueCollection that you are never supposed to use directly in your code. Another property of HttpValueCollection is that it automatically url encodes values when you add them to this collection.

    Here's how to use the HttpValueCollection class:

    class Program
    {
        static void Main()
        {
            // returns an implementation of NameValueCollection
            // which in fact is HttpValueCollection
            var values = HttpUtility.ParseQueryString(string.Empty);
            values["param1"] = "v&=+alue1";
            values["param2"] = "value2";*
    
            // prints "param1=v%26%3d%2balue1¶m2=value2"
            Console.WriteLine(values.ToString());
        }
    }
    

提交回复
热议问题