What is IEnumerable?
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
Syntax:
public interface IEnumerable
IEnumerable Methods
Below are the methods of IEnumerable C#:
Cast<TResult>(IEnumerable): The non-generic collection of the IEnumerable interface is converted to the specified type mentioned.
OfType<TResult>(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.
What is ICollection?
ICollection is an interface, you can’t instantiate it directly. You’ll need to instantiate a class that implements ICollection; for example, List<T>. Also, the ICollection interface doesn’t have an Add method — you’ll need something that implements IList or IList<T> for that.
Syntax:
public interface ICollection<T> : System.Collections.Generic.IEnumerable<T>
ICollection Properties
The following are the properties of ICollection interface −
Count - The number of elements in the ICollection
SyncRoot - Gets an object that useful to synchronize access to the ICollection.
ICollection Methods
The following are the methods of ICollection interface −
CopyTo(Array^,Int32) - The method copies the elements of the ICollection to an Array.
GetEnumerator() - The GetEnumerator() method returns an enumerator that iterates through a collection
What is IList?
IList is an interface that represents a collection of objects which can be accessed by index. List<T> is a concrete implementation of IList<T> interface. In OOP, it is a good programming practice to use interfaces than using concrete classes. Therefore, the programmer can use IList>T> type variable to create an object of List<T>.
Its implementation is generally categorized into three parts that are:
read-only: In which the IList cannot be modified.
fixed-size: In which IList is not allowed to add or remove elements.
variable size: In which IList is allowed to modify, add, or remove elements.
Syntax:
// For Non_generic IList
public interface IList : System.Collections.ICollection
// For Generic IList
public interface IList<T> : System.Collections.Generic.ICollection<T>,
System.Collections.Generic.IEnumerable<T>
IList Properties:
Some of the commonly used properties of the IList interface are:
Count - This property will return the total number of items present in the ICollection.
IsFixedSize - This property will return a value that determines whether the IList has a fixed size or not.
IsReadOnly - This property will return a value that determines whether the IList is read-only or not.
IsSynchronized - This property will return a value that determines whether access to the ICollection is synchronized or not.
Item[Int32] - This property will gets or sets the item from the specified index.
SyncRoot - This property will get an object that can be used to synchronize access to the ICollection.
IList Methods:
Some of the commonly used methods of the IList interface are:
Add(Object) - This method is used to add an item to the IList.
Clear() - This method is used to remove or delete all items from the IList.
Contains(Object) - This method is used to check whether the IList contains a specific value or not.
CopyTo(Array, Int32) - This method is used to copy the elements of the ICollection to an array, starting from the given index.
GetEnumerator() - This method is used to get an enumerator that iterates through a collection.
IndexOf(Object) - This method is used to find the index of a specific item in the IList.
Insert(Int32, Object) - This method is used to insert an item to the IList at the given index.
Remove(Object) - This method is used to remove or eliminate the first occurrence of a given object from the IList.
RemoveAt(Int32) - This method is used to remove the IList item from the given index.
What is IQueryable?
IQueryable is useful when we want to iterate a collection of objects which deals with ad-hoc queries against the data source or remote database, like SQL Server. The IQueryable is an interface and it is available in System.Linq namespace. The IQuerable interface is a child of the IEnumerable interface. So we can store IQuerable in a variable of type IEnumerable The IQuerable interface has a property called Provider which is of type IQueryProvider interface. Let us see the definition of IQueryProvider.
Key Points:
IQueryable exists in System. Linq Namespace.
IQueryable can move forward only over a collection, it can’t move backward and between the items.
IQueryable is best to query data from out-memory (like remote database, service) collections.
While query data from a database, IQueryable execute the select query on the server side with all filters.
IQueryable is suitable for LINQ to SQL queries.
IQueryable supports deferred execution.
IQueryable supports custom query using CreateQuery and Execute methods.
IQueryable support lazy loading. Hence it is suitable for paging like scenarios.
Extension methods support by IQueryable takes expression objects means expression tree.
Syntax:
public interface IQueryable : System.Collections.IEnumerable
Properties
ElementType - Gets the type of the element(s) that are returned when the expression tree associated with this instance of IQueryable is executed.
Expression - Gets the expression tree that is associated with the instance of IQueryable.
Provider - Gets the query provider that is associated with this data source.
Methods
GetEnumerator() - Returns an enumerator that iterates through a collection.
Extension Methods
Cast<TResult>(IEnumerable) - Casts the elements of an IEnumerable to the specified type.
OfType<TResult>(IEnumerable) - Filters the elements of an IEnumerable based on a specified type.
AsParallel(IEnumerable) - Enables parallelization of a query.
AsQueryable(IEnumerable) - Converts an IEnumerable to an IQueryable.
Cast<TResult>(IQueryable) - Converts the elements of an IQueryable to the specified type.
OfType<TResult>(IQueryable) - Filters the elements of an IQueryable based on a specified type.
The Tech Platform
Comments