Home  >  Article  >  Backend Development  >  IEnumerable C#

IEnumerable C#

WBOY
WBOYOriginal
2024-09-03 15:32:12424browse

IEnumerable is an interface, which defines only one method, GetEnumerator. The method returns an IEnumerator interface. This interface is used to iterate lists or collection of classes that are of anonymous types. It contains the System. Collections.Generic namespace. It is used to work with LINQ query expression as well. This allows only a read only access to the collection, then a collection that inherits the main collection can be iterated using a for-each loop. The IEnumerable interface is the base interface for all non-generic lists. There are four extension methods of IEnumerable interfaces. They are AsParallel(), AsQueryable(), Cast(), OfType(). This article will explain in detail about the IEnumerable interface along with various examples.

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:

  • Cast(IEnumerable): The non-generic collection of the IEnumerable interface is converted to the specified type mentioned.
  • OfType(IEnumerable): The elements of the IEnumerable are filtered based on the type mentioned.
  • 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++;
}
// 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:

IEnumerable C#

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:

IEnumerable C#

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()
{
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:

IEnumerable C#

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
};
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:

IEnumerable C#

Example #5

Code:

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:

IEnumerable C#

Conclusion

Thus, the article explained in detail about IEnumerable in C#. It also explained the various methods like GetEnumerator and other extended methods that are associated with IEnumerable like AsParallel(), AsQueryable(), Cast(), OfType() in detail. It also demonstrated with example the use of current, next, and reset methods. To learn more in detail it would be advisable to write sample programs and practice them.

The above is the detailed content of IEnumerable C#. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:C# DictionaryNext article:C# Dictionary