Trending September 2023 # Top 5 Examples To Implement Of Ienumerable C# # Suggested October 2023 # Top 13 Popular | Lifecanntwaitvn.com

Trending September 2023 # Top 5 Examples To Implement Of Ienumerable C# # Suggested October 2023 # Top 13 Popular

You are reading the article Top 5 Examples To Implement Of Ienumerable C# updated in September 2023 on the website Lifecanntwaitvn.com. We hope that the information we have shared is helpful to you. If you find the content interesting and meaningful, please share it with your friends and continue to follow and support us for the latest updates. Suggested October 2023 Top 5 Examples To Implement Of Ienumerable C#

Introduction to IEnumerable C# Syntax of IEnumerable C#

The syntax is as follows:

public interface IEnumerable

The collection is iterated using the movenext() and reset() methods.

Extension Methods in IEnumerable C#

Below are the methods:

AsParallel(IEnumerable): This is used to enable the running of parallel queries.

AsQueryable(IEnumerable): This is used to convert IEnumerable interface to IQueryable interface.

Examples to Implement of IEnumerable C#

Below are the examples:

Example #1

Code:

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++; } 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 + "tt" + "lastnme :" + obj.lastnme); } Console.ReadLine(); } } }

Output:

Example #2

Code:

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:

Example #3

Code:

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() { { new testclass { eid= 1001, ename = "viki", salary = 1000 }, new testclass { eid= 1002, ename = "nandhini", salary = 600 }, new testclass { eid= 1003, ename = "vyapinin", salary = 10000 } }; { Console.WriteLine("eid" + chúng tôi + " ename : " + kvp.Value.ename + ", salary: " + kvp.Value.salary); } Console.ReadKey(); } } }

Output:

Example #4

Code:

using System; using System.Collections.Generic; using System.Linq; class Program { static void Main() { int[] numbers = new int[] { 1,13,51,75 }; { Console.WriteLine(pair); } { "vignesh","jagan","nyan","ravi","siva","sethu" }; if (result.ContainsKey("jagan")) { Console.WriteLine("name exists"); } } }

Example #5

Code:

using System; using System.Collections.Generic; using System.Linq; namespace LINQDemo { class Program { static void Main(string[] args) { { 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} }; 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:

Conclusion Recommended Articles

This is a guide to IEnumerable C#. Here we discuss a brief overview on IEnumerable C# and its Examples along with its Code Implementation. You can also go through our other suggested articles to learn more –

You're reading Top 5 Examples To Implement Of Ienumerable C#

Update the detailed information about Top 5 Examples To Implement Of Ienumerable C# on the Lifecanntwaitvn.com website. We hope the article's content will meet your needs, and we will regularly update the information to provide you with the fastest and most accurate information. Have a great day!