怎样实现foreach啊?
提示: 作者被禁止或删除 内容自动屏蔽
using System; using System.Collections.Generic; using System.Collections; using PairOfString = System.Collections.Generic.KeyValuePair<string, string>; namespace ConsoleApplication1 { class Program { public class student : IEnumerable<PairOfString> { private string no; private string name; private string sex; private string hobby; public student() { no = "20010"; name = "baomi"; sex = "male"; hobby = "computer game"; } public IEnumerator<PairOfString> GetEnumerator() { yield return new PairOfString("no", no); yield return new PairOfString("name", name); yield return new PairOfString("sex", sex); yield return new PairOfString("hobby", hobby); } IEnumerator IEnumerable.GetEnumerator() { return (this as IEnumerable<PairOfString>).GetEnumerator(); } } static void Main(string[] args) { student sd = new student(); foreach (var item in sd) { Console.WriteLine(item.Key + "=" + item.Value); } Console.ReadKey(); } } }