我是一個典型的新手...無效的轉換異常,同時使用LINQ
我可以編譯我的程序,但在運行PROGRAMM
有什麼錯我的代碼時,你得到的invalidcast錯誤時拋出。我不沒有什麼SEACH爲:(
這是我的代碼(.NET 3.5)
class Element{
private int i;
public int I { get { return i; } set { i = value; } }
private string s;
public string S { get { return s; } set { s = value; } }
public Element(string _s, int _i) {
this.s = _s;
this.i = _i;
}
}
class myDict : Dictionary<string, Element> {
public void afunction() {
}
}
class Program {
static void Main(string[] args) {
myDict myDict = new myDict();
myDict.Add("a", new Element("x", 23));
myDict.Add("b", new Element("y", 48));
var sortedDict = ((from entry in myDict orderby entry.Key descending select entry).Take(10));
myDict = (myDict)sortedDict.ToDictionary(v => v.Key, v => v.Value);
foreach (KeyValuePair<string, Element> kvp in myDict) {
System.Console.WriteLine("-> " +kvp.Key + " " + kvp.Value);
}
Console.ReadLine();
}
}
謝謝!邁克爾:)
不要繼承BCL集合類,例如'List'和'Dictionary '。 –