System.Collections.Specialized.NameObjectCollectionBase有两个类似的属性:
string[] AllKeys
NameObjectCollectionBase.KeyCollection Keys
Run Code Online (Sandbox Code Playgroud)
他们提供不同的数据集吗?我什么时候想要使用另一个?
Nei*_*ams 61
AllKeys是一个O(n)操作,Keys而是O(1).这是因为AllKeys将密钥复制到新数组中,而Keys只返回对NameValueCollection私钥集合的引用.因此,除了性能上的差异之外,返回的集合Keys将随基本集合而变化,因为它只是对原始集合的引用,而AllKeys与变更绝缘,因为它是副本.
这个小测试程序显示了行为的差异:
using System;
using System.Collections.Specialized;
static class Program
{
static void Main()
{
var collection = new NameValueCollection();
var keys = collection.Keys;
var allKeys = collection.AllKeys;
collection.Add("Name", "Value");
Console.WriteLine("Keys: " + keys.Count);
Console.WriteLine("AllKeys: " + allKeys.Length);
Console.ReadLine();
}
}
Run Code Online (Sandbox Code Playgroud)
输出是:
Keys: 1
AllKeys: 0
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3843 次 |
| 最近记录: |