Bop*_*Bop 3 .net c# collections
我有这个代码(我希望它可以工作,但它失败了)..我真的不知道为什么.请帮忙
static void Main(string[] args)
{
var x = new MyKeyedCollection();
x.Add(new MyType() { Key = 400L, Value = 0.1 });
x.Add(new MyType() { Key = 200L, Value = 0.1 });
x.Add(new MyType() { Key = 100L, Value = 0.1 });
x.Add(new MyType() { Key = 300L, Value = 0.1 });
//foreach (var item in x)
for (int i = 0; i < x.Count; i++)
{
//Debug.WriteLine(item.PriceLevel);
Debug.WriteLine(x[i].Key);
}
}
}
public class MyType
{
public long Key;
public double Value;
}
public class MyKeyedCollection : KeyedCollection<long, MyType>
{
protected override long GetKeyForItem(MyType item)
{
return item.Key;
}
}
Run Code Online (Sandbox Code Playgroud)
例外:
System.Collections.Generic.KeyNotFoundException未处理
Message =给定的键不在字典中.
Source = mscorlib StackTrace:在System.Collections.Generic.Dictionary的System.ThrowHelper.ThrowKeyNotFoundException()中2.get_Item(TKey key) at System.Collections.ObjectModel.KeyedCollection2. 在KeyedCollectionTest.Program.Main(String [] args)中的get_Item(TKey键) ...\Program.cs:line System.Threading的System.Threading.ExecutionContext.Run(ExecutionContext executionContext,ContextCallback callback,Object state)上的Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()中的System.AppDomain._nExecuteAssembly(程序集,String [] args)中的25 .ThreadHelper.ThreadStart()InnerException:
为什么它试图获得Key而不是索引?键明显很长而不是int.我确信之前我使用过KeyedCollection,它对long key和int index工作得很好.
我尝试在ver 2,3,4,4,4.5中编译(使用VS2012)...
别搞定.
为什么它试图获得Key而不是索引?键明显很长而不是int.
但是int可以转换为long,所以它是一个有效的候选成员.
问题是this[TKey key]索引器最初是在声明的KeyedCollection,而this[int index]索引器最初是在声明的Collection.重载决策规则指定首先搜索最派生的类,并且只考虑首先在该类型中声明的成员.只有当该搜索失败并编译器拉升到类型层次结构的一个新的水平.
所以,如果你写:
Collection<MyType> collection = x;
for (int i = 0; i < x.Count; i++)
{
Debug.WriteLine(collection[i].Key);
}
Run Code Online (Sandbox Code Playgroud)
它会工作 - 因为编译时类型collection只是Collection<T>,它只有 "int index"索引器.
这是一个示例,它显示了相同的行为,而不使用泛型,索引器或抽象类:
using System;
class Base
{
public void Foo(int x)
{
Console.WriteLine("Base.Foo(int)");
}
}
class Derived : Base
{
public void Foo(long y)
{
Console.WriteLine("Derived.Foo(long)");
}
}
class Program
{
static void Main()
{
Derived x = new Derived();
Base y = x;
x.Foo(5); // Derived.Foo(long)
y.Foo(5); // Base.Foo(int)
}
}
Run Code Online (Sandbox Code Playgroud)
请参阅我关于重载的文章,了解更多有趣的规则.
| 归档时间: |
|
| 查看次数: |
2127 次 |
| 最近记录: |