当前位置: 首页>>代码示例>>C#>>正文


C# NameValueCollection.HasKeys方法代码示例

本文整理汇总了C#中NameValueCollection.HasKeys方法的典型用法代码示例。如果您正苦于以下问题:C# NameValueCollection.HasKeys方法的具体用法?C# NameValueCollection.HasKeys怎么用?C# NameValueCollection.HasKeys使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在NameValueCollection的用法示例。


在下文中一共展示了NameValueCollection.HasKeys方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: ToQueryString

    public static string ToQueryString(NameValueCollection collection, bool startWithQuestionMark = true)
    {
        if (collection == null || !collection.HasKeys())
            return String.Empty;

        var sb = new StringBuilder();
        if (startWithQuestionMark)
            sb.Append("?");

        var j = 0;
        var keys = collection.Keys;
        foreach (string key in keys)
        {
            var i = 0;
            var values = collection.GetValues(key);
            foreach (var value in values)
            {
                sb.Append(key)
                    .Append("=")
                    .Append(value);

                if (++i < values.Length)
                    sb.Append("&");
            }
            if (++j < keys.Count)
                sb.Append("&");
        }
        return sb.ToString();
    }
开发者ID:timw255,项目名称:Sitefinity-STS-Modification,代码行数:29,代码来源:SimpleWebTokenHandler.cs

示例2: Add

        public void Add()
        {
            NameValueCollection nameValueCollection = new NameValueCollection();
            Assert.False(nameValueCollection.HasKeys());
            for (int i = 0; i < 10; i++)
            {
                string name = "Name_" + i;
                string value = "Value_" + i;
                nameValueCollection.Add(name, value);
                Assert.Equal(i + 1, nameValueCollection.Count);
                Assert.Equal(i + 1, nameValueCollection.AllKeys.Length);
                Assert.Equal(i + 1, nameValueCollection.Keys.Count);

                // We should be able to access values by the name
                Assert.Equal(value, nameValueCollection[name]);
                Assert.Equal(value, nameValueCollection.Get(name));

                Assert.Contains(name, nameValueCollection.AllKeys);
                Assert.Contains(name, nameValueCollection.Keys.Cast<string>());

                Assert.Equal(new string[] { value }, nameValueCollection.GetValues(name));

                // Get(string), GetValues(string) and this[string] should be case insensitive
                Assert.Equal(value, nameValueCollection[name.ToUpperInvariant()]);
                Assert.Equal(value, nameValueCollection[name.ToLowerInvariant()]);

                Assert.Equal(value, nameValueCollection.Get(name.ToUpperInvariant()));
                Assert.Equal(value, nameValueCollection.Get(name.ToLowerInvariant()));

                Assert.Equal(new string[] { value }, nameValueCollection.GetValues(name.ToUpperInvariant()));
                Assert.Equal(new string[] { value }, nameValueCollection.GetValues(name.ToLowerInvariant()));

                Assert.DoesNotContain(name.ToUpperInvariant(), nameValueCollection.AllKeys);
                Assert.DoesNotContain(name.ToLowerInvariant(), nameValueCollection.AllKeys);

                Assert.DoesNotContain(name.ToUpperInvariant(), nameValueCollection.Keys.Cast<string>());
                Assert.DoesNotContain(name.ToLowerInvariant(), nameValueCollection.Keys.Cast<string>());

                // We should be able to access values and keys in the order they were added
                Assert.Equal(value, nameValueCollection[i]);
                Assert.Equal(value, nameValueCollection.Get(i));
                Assert.Equal(name, nameValueCollection.GetKey(i));
                Assert.Equal(new string[] { value }, nameValueCollection.GetValues(i));
            }
            Assert.True(nameValueCollection.HasKeys());
        }
开发者ID:ChuangYang,项目名称:corefx,代码行数:46,代码来源:NameValueCollection.AddStringStringTests.cs

示例3: Add_NullName

        public void Add_NullName()
        {
            NameValueCollection nameValueCollection = new NameValueCollection();
            string value = "value";
            nameValueCollection.Add(null, value);
            Assert.Equal(1, nameValueCollection.Count);
            Assert.Equal(1, nameValueCollection.AllKeys.Length);
            Assert.Equal(1, nameValueCollection.Keys.Count);

            Assert.Contains(null, nameValueCollection.AllKeys);
            Assert.Contains(null, nameValueCollection.Keys.Cast<string>());

            Assert.Equal(value, nameValueCollection[null]);
            Assert.Equal(value, nameValueCollection.Get(null));

            Assert.Equal(value, nameValueCollection[0]);
            Assert.Equal(value, nameValueCollection.Get(0));

            Assert.Equal(new string[] { value }, nameValueCollection.GetValues(null));
            Assert.Equal(new string[] { value }, nameValueCollection.GetValues(0));

            Assert.Null(nameValueCollection.GetKey(0));
            Assert.False(nameValueCollection.HasKeys());
        }
开发者ID:ChuangYang,项目名称:corefx,代码行数:24,代码来源:NameValueCollection.AddStringStringTests.cs

示例4: runTest

 public virtual bool runTest()
 {
     Console.WriteLine(s_strTFPath + " " + s_strTFName + " , for " + s_strClassMethod + " , Source ver: " + s_strDtTmVer);
     int iCountErrors = 0;
     int iCountTestcases = 0;
     IntlStrings intl;
     String strLoc = "Loc_000oo";
     NameValueCollection nvc; 
     string [] values = 
     {
         "",
         " ",
         "a",
         "aA",
         "text",
         "     SPaces",
         "1",
         "$%^#",
         "2222222222222222222222222",
         System.DateTime.Today.ToString(),
         Int32.MaxValue.ToString()
     };
     string [] keys = 
     {
         "zero",
         "oNe",
         " ",
         "",
         "aa",
         "1",
         System.DateTime.Today.ToString(),
         "$%^#",
         Int32.MaxValue.ToString(),
         "     spaces",
         "2222222222222222222222222"
     };
     int cnt = 0;            
     try
     {
         intl = new IntlStrings(); 
         Console.WriteLine("--- create collection ---");
         strLoc = "Loc_001_oo"; 
         iCountTestcases++;
         nvc = new NameValueCollection();
         cnt = nvc.Count;
         if ( cnt != 0) 
         {
             iCountErrors++;
             Console.WriteLine("Err_0001a, count is {0} instead of {1} after default ctor", nvc.Count, 0);
         }
         Console.WriteLine("1. call HasKeys() on empty collection");
         strLoc = "Loc_001oa"; 
         iCountTestcases++;
         if ( nvc.HasKeys() ) 
         {
             iCountErrors++;
             Console.WriteLine("Err_0001b, HasKeys returned true after default ctor");
         }
         Console.WriteLine("2. add simple strings and call HasKeys()");
         strLoc = "Loc_002oo"; 
         iCountTestcases++;
         cnt = nvc.Count;
         for (int i = 0; i < values.Length; i++) 
         {    
             nvc.Add(keys[i], values[i]);
         }
         if (nvc.Count != values.Length) 
         {
             iCountErrors++;
             Console.WriteLine("Err_0002a, count is {0} instead of {1}", nvc.Count, values.Length);
         }
         iCountTestcases++;
         if ( !nvc.HasKeys() ) 
         {
             iCountErrors++;
             Console.WriteLine("Err_0002b, returned false for collection with {0} items", nvc.Count);
         }
         Console.WriteLine("3. add intl strings and call HasKeys()");
         strLoc = "Loc_003oo"; 
         int len = values.Length;
         string [] intlValues = new string [len * 2];
         for (int i = 0; i < len * 2; i++) 
         {
             string val = intl.GetString(MAX_LEN, true, true, true);
             while (Array.IndexOf(intlValues, val) != -1 )
                 val = intl.GetString(MAX_LEN, true, true, true);
             intlValues[i] = val;
         } 
         cnt = nvc.Count;
         iCountTestcases++;
         for (int i = 0; i < len; i++) 
         {    
             nvc.Add(intlValues[i+len], intlValues[i]);
         }
         if (nvc.Count != (cnt + len)) 
         {
             iCountErrors++;
             Console.WriteLine("Err_0003a, count is {0} instead of {1}", nvc.Count, cnt + len);
         }
         iCountTestcases++;
//.........这里部分代码省略.........
开发者ID:ArildF,项目名称:masters,代码行数:101,代码来源:co8730haskeys.cs

示例5: Add_NullValue

        public void Add_NullValue()
        {
            NameValueCollection nameValueCollection = new NameValueCollection();
            string name = "name";
            nameValueCollection.Add(name, null);
            Assert.Equal(1, nameValueCollection.Count);
            Assert.Equal(1, nameValueCollection.AllKeys.Length);
            Assert.Equal(1, nameValueCollection.Keys.Count);

            Assert.Contains(name, nameValueCollection.AllKeys);
            Assert.Contains(name, nameValueCollection.Keys.Cast<string>());

            Assert.Null(nameValueCollection[name]);
            Assert.Null(nameValueCollection.Get(name));

            Assert.Null(nameValueCollection[0]);
            Assert.Null(nameValueCollection.Get(0));

            Assert.Null(nameValueCollection.GetValues(name));
            Assert.Null(nameValueCollection.GetValues(0));

            Assert.Equal(name, nameValueCollection.GetKey(0));
            Assert.True(nameValueCollection.HasKeys());
        }
开发者ID:ChuangYang,项目名称:corefx,代码行数:24,代码来源:NameValueCollection.AddStringStringTests.cs

示例6: Add_AddingValueToExistingName_AppendsValueToOriginalValue

        public void Add_AddingValueToExistingName_AppendsValueToOriginalValue()
        {
            var nameValueCollection = new NameValueCollection();
            string name = "name";
            nameValueCollection.Add(name, "value1");
            nameValueCollection.Add(name, "value2");
            nameValueCollection.Add(name, null);

            Assert.Equal(1, nameValueCollection.Count);
            Assert.Equal(1, nameValueCollection.AllKeys.Length);
            Assert.Equal(1, nameValueCollection.Keys.Count);

            Assert.Contains(name, nameValueCollection.AllKeys);
            Assert.Contains(name, nameValueCollection.Keys.Cast<string>());

            string[] expected = new string[] { "value1", "value2" };
            string expectedString = string.Join(",", expected);

            Assert.Equal(expectedString, nameValueCollection[name]);
            Assert.Equal(expectedString, nameValueCollection.Get(name));

            Assert.Equal(expectedString, nameValueCollection[0]);
            Assert.Equal(expectedString, nameValueCollection.Get(0));

            Assert.Equal(expected, nameValueCollection.GetValues(name));
            Assert.Equal(expected, nameValueCollection.GetValues(0));

            Assert.Equal(name, nameValueCollection.GetKey(0));
            Assert.True(nameValueCollection.HasKeys());
        }
开发者ID:ChuangYang,项目名称:corefx,代码行数:30,代码来源:NameValueCollection.AddStringStringTests.cs


注:本文中的NameValueCollection.HasKeys方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。