1
我有下面的代碼,我正在嘗試使用Foreach工作。我正在玩C#所以非常初學,但似乎無法讓IEnumerable位工作?有人可以告訴我如何可以循環我的列表屬性。在SortedList上啓用foreach
using System;
using System.Collections;
using System.Collections.Generic;
namespace MyApplication
{
class arrayLess : IEnumerable {
SortedList<string,dynamic> list = new SortedList<string,dynamic>();
// Will need to update global list of array_values
// This will be a proper array
public void add(dynamic key,dynamic val){
list[key.ToString()] = val;
}
// Could be int in which case need a list that isn't a sortedList?
public dynamic get(dynamic key){
return list[key.ToString()];
}
IEnumerator<string> IEnumerable<string>.GetEnumerator(){
Console.WriteLine("HERE");
return this.list.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator(){
return this.list.GetEnumerator();
}
}
class launcher {
static void Main(string[] args){
helloWorld HW = new helloWorld();
HW.test();
}
}
class helloWorld {
// Define the variable and type
arrayLess resultSet;
public void test(){
resultSet = new arrayLess();
resultSet.add("test",new arrayLess());
resultSet.add(5,"Does this work?");
resultSet.get("test").add("new","value");
Console.WriteLine(resultSet.get("test").get("new"));
Console.WriteLine(resultSet.get(5));
foreach(string key in resultSet){
Console.WriteLine(key);
}
}
}
}
哪裏去對不起? – UKUser35
@ UKUser35在'的IEnumerator的IEnumerable .GetEnumerator()'和'IEnumerable.GetEnumerator()' –
@ UKUser35注意,您的類還應該說'的頂部:IEnumerable的','不是:IEnumerable' –