本文整理汇总了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();
}
示例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());
}
示例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());
}
示例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++;
//.........这里部分代码省略.........
示例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());
}
示例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());
}