IEnumerable 是一个接口,它只定义了一个方法:GetEnumerator。该方法返回一个 IEnumerator 接口。此接口用于迭代匿名类型的类的列表或集合。它包含系统。 Collections.Generic 命名空间。它也用于与 LINQ 查询表达式一起使用。这仅允许对集合进行只读访问,然后可以使用 for-each 循环迭代继承主集合的集合。 IEnumerable 接口是所有非通用列表的基本接口。 IEnumerable 接口有四种扩展方法。它们是 AsParallel()、AsQueryable()、Cast
语法如下:
public interface IEnumerable
使用 movenext() 和 reset() 方法迭代集合。
方法如下:
以下是示例:
代码:
using System; using System.Diagnostics; using System.Reflection; using System.Collections; namespace TestEg { class Test : IEnumerable { Test[] coll = null; int Fi = 0; public String Fname { get; set; } public string lastnme { get; set; } public Test() { coll = new Test[10]; } public void Add(Test item) { coll[Fi] = item; Fi++; } // IEnumerable Member public IEnumeratorGetEnumerator() { foreach (object o in coll) { if(o == null) { break; } yield return o; } } } class Program { public static void Main(String[] args) { Test tobj = new Test(); tobj.Fname = "viki"; tobj.lastnme = "krish"; Test tobj1 = new Test(); tobj1.Fname = "nand"; tobj1.lastnme = "viki"; Test myList = new Test(); Test tobj2 = new Test(); tobj2.Fname = "vyapini"; tobj2.lastnme = "viki"; Test tobj3 = new Test(); tobj3.Fname = "tai"; tobj3.lastnme = "viki"; myList.Add(tobj); myList.Add(tobj1); myList.Add(tobj2); myList.Add(tobj3); foreach (Test obj in myList) { Console.WriteLine("Fname:" + obj.Fname + "\t\t" + "lastnme :" + obj.lastnme); } Console.ReadLine(); } } }
输出:
代码:
using System; using System.Diagnostics; using System.Reflection; using System.Collections; public class test : IEnumerable { public string Student1 { get; set; } public string Student2 { get; set; } public string Student3 { get; set; } public IEnumeratorGetEnumerator() { return new testEnumerator(this); } } public class testEnumerator : IEnumerator { public testEnumerator(test ts) { _ts = ts; } private test _ts; private int _index = 0; public void Reset() { _index = 0; Current = null; } public object Current { get; private set; } public bool MoveNext() { _index++; /**/ if (_index == 1) { Current = _ts.Student1; return true; } else if (_index == 2) { Current = _ts.Student2; return true; } else if (_index == 3) { Current = _ts.Student3; return true; } else return false; } } class Program { public static void Main(String[] args) { varts = new test() {Student1 = "vignesh", Student2 = "nandhini", Student3 = "vyapini"}; foreach (string name in ts) { Console.WriteLine(name); } } }
输出:
代码:
using System.Linq; using System.Collections.Generic; using System; namespace TestOper { public class testclass { public inteid { get; set; } public string ename { get; set; } public double salary { get; set; } } class Program { public static void Main() { List<testclass>listtestclasss = new List<testclass> { new testclass { eid= 1001, ename = "viki", salary = 1000 }, new testclass { eid= 1002, ename = "nandhini", salary = 600 }, new testclass { eid= 1003, ename = "vyapinin", salary = 10000 } }; Dictionary<int, testclass>empdic = listtestclasss.ToDictionary(x =>x.eid); foreach (KeyValuePair<int, testclass>kvp in empdic) { Console.WriteLine("eid" + kvp.Key + " ename : " + kvp.Value.ename + ", salary: " + kvp.Value.salary); } Console.ReadKey(); } } }
输出:
代码:
using System; using System.Collections.Generic; using System.Linq; class Program { static void Main() { int[] numbers = new int[] { 1,13,51,75 }; Dictionary<int, bool>dic = numbers.ToDictionary(v => v, v => true); foreach (KeyValuePair<int, bool> pair in dic) { Console.WriteLine(pair); } List<string> names = new List<string>() { "vignesh","jagan","nyan","ravi","siva","sethu" }; var result = names.ToDictionary(x => x, x => true); if (result.ContainsKey("jagan")) { Console.WriteLine("name exists"); } } }
输出:
代码:
using System; using System.Collections.Generic; using System.Linq; namespace LINQDemo { class Program { static void Main(string[] args) { List<emp>sl = new List<emp>() { new emp(){empid = 1, empname = "James", empgender = "Male",eage=60}, new emp(){empid = 2, empname = "Sara", empgender = "Female",eage=90}, new emp(){empid = 3, empname = "Steve", empgender = "Male",eage=80}, new emp(){empid = 4, empname = "Pam", empgender = "Female",eage=70}, new emp(){empid = 5, empname = "James", empgender = "Male",eage=60}, new emp(){empid = 6, empname = "Sara", empgender = "Female",eage=50}, new emp(){empid = 7, empname = "Steve", empgender = "Male",eage=20}, new emp(){empid = 8, empname = "Pam", empgender = "Female",eage=40} }; IQueryable<emp>iq = sl.AsQueryable() .Where(t =>t.eage> 40); foreach (varemp in iq) { Console.WriteLine( $"empid : {emp.empid} empname : {emp.empname} eage : {emp.eage} empgender : {emp.empgender} "); } Console.ReadKey(); } } public class emp { public intempid { get; set; } public string empname { get; set; } public string empgender { get; set; } public inteage { get; set; } } }
输出:
至此,文章详细讲解了C#中的IEnumerable。它还详细解释了 GetEnumerator 等各种方法以及与 IEnumerable 相关的其他扩展方法,如 AsParallel()、AsQueryable()、Cast
以上是IE可枚举 C#的详细内容。更多信息请关注PHP中文网其他相关文章!