Rumah >pembangunan bahagian belakang >Tutorial C#.Net >IEnumerable C#
IEnumerable ialah antara muka, yang mentakrifkan hanya satu kaedah, GetEnumerator. Kaedah ini mengembalikan antara muka IEnumerator. Antara muka ini digunakan untuk mengulang senarai atau koleksi kelas yang jenis tanpa nama. Ia mengandungi Sistem. Koleksi.Ruang nama generik. Ia digunakan untuk berfungsi dengan ungkapan pertanyaan LINQ juga. Ini hanya membenarkan akses baca sahaja kepada koleksi, kemudian koleksi yang mewarisi koleksi utama boleh diulang menggunakan gelung untuk setiap. Antara muka IEnumerable ialah antara muka asas untuk semua senarai bukan generik. Terdapat empat kaedah sambungan antara muka IEnumerable. Mereka ialah AsParallel(), AsQueryable(), Cast
Sintaks adalah seperti berikut:
public interface IEnumerable
Koleksi diulang menggunakan kaedah movenext() dan reset().
Di bawah adalah kaedahnya:
Di bawah ialah contoh:
Kod:
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(); } } }
Output:
Kod:
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); } } }
Output:
Kod:
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(); } } }
Output:
Kod:
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"); } } }
Output:
Kod:
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; } } }
Output:
Oleh itu, artikel tersebut menerangkan secara terperinci tentang IEnumerable dalam C#. Ia juga menerangkan pelbagai kaedah seperti GetEnumerator dan kaedah lanjutan lain yang dikaitkan dengan IEnumerable seperti AsParallel(), AsQueryable(), Cast
Atas ialah kandungan terperinci IEnumerable C#. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!