Home > DeveloperSection > Forums > What is IEnumerable In C#.
Ankit Singh

Total Post:341

Points:2389
Posted on    November-06-2015 10:31 PM

 .NET C# 
Ratings:


 1 Reply(s)
 291  View(s)
Rate this:

  I want to use IEnumerable In C# please give me solutions



aditya kumar Patel

Total Post:244

Points:1736
Posted on    November-06-2015 10:45 PM

The IEnumerable<T> is used to iterate a read only collection. It has only one method GetEnumeartor() which allows you to iterate the read only collection using a foreach loop. It only iterates in the forward direction.  IEnumerable <T> is defined in as follows:

using System;

using System.Collections.Generic;

using System.Diagnostics;

using System.Linq;

using System.Net;

using System.Text;

using System.Threading;

namespace Forum

{

public class ChildClass

{

static void Main(string[] args)

{

List<Customer> CustomerList = new List<Customer>();

Customer obj = new Customer();

obj.CustomerID = 1;

obj.CustomerName = "Visal";

obj.ContactNumber = "8787878787";

CustomerList.Add(obj);

obj = new Customer();

obj.CustomerID = 1;

obj.CustomerName = "Visal";

obj.ContactNumber = "9797979797";

CustomerList.Add(obj);

obj = new Customer();

obj.CustomerID = 1;

obj.CustomerName = "Visal";

obj.ContactNumber = "77986521322";

CustomerList.Add(obj);

IEnumerable<Customer> EnumList = CustomerList;

foreach(Customer o in EnumList)

{

Console.WriteLine("CustomerID: {0}",o.CustomerID);

Console.WriteLine("CustomerName: {0}", o.CustomerName);

Console.WriteLine("ContactNumber: {0}", o.ContactNumber +"\n");

}

Console.ReadLine();

}

}

public class Customer

{

public int CustomerID { get; set; }

public String CustomerName { get; set; }

public string ContactNumber { get; set; }

}

}

 


Don't want to miss updates? Please click the below button!

Follow MindStick