Query Result Types

Level : Intermediate
Mentor: Shailendra Chauhan
Duration : 00:01:00

IEnumerable

The IEnumerable interface provides a generic way to iterate through a collection. It exposes a single method, GetEnumerator(), which returns an enumerator that can be used to loop through the elements in the collection.

Example

IEnumerable<string> colors = new List<string> { "Red", "Green", "Blue" };
foreach (string color in colors)
{
  Console.WriteLine(color);
}

IQueryable

The IQueryable interface is used for querying data from a data source, typically a database, by defining a query using expressions and providing an IQueryProvider. This allows for deferred execution of queries.

Example

IQueryable<Employee> query = dbContext.Employees.Where(e => e.Department == "HR");
List<Employee> hrEmployees = query.ToList();

ICollection

The ICollection interface extends IEnumerable and adds methods for managing a collection, such as Count, Contains, Add, and Remove.

Example

ICollection<string> colors = new List<string> { "Red", "Green", "Blue" };
int count = colors.Count;
bool containsGreen = colors.Contains("Green");
colors.Add("Yellow");
colors.Remove("Red");

IList

The IList interface extends ICollection and provides additional methods for working with a list, including IndexOf, Insert, and RemoveAt.

Example

IList<string> fruits = new List<string> { "Apple", "Banana", "Cherry" };
int cherryIndex = fruits.IndexOf("Cherry");
fruits.Insert(1, "Orange");
fruits.RemoveAt(0);

List

List is a concrete class in C# that implements the IList interface. It provides a dynamic array that can be used to store and manipulate elements efficiently.

Example

List<int> numbers = new List<int> { 1, 2, 3, 4, 5 };
numbers.Add(6);
numbers.Remove(2);
int thirdNumber = numbers[2];
Self-paced Membership
  • 22+ Video Courses
  • 800+ Hands-On Labs
  • 400+ Quick Notes
  • 55+ Skill Tests
  • 45+ Interview Q&A Courses
  • 10+ Real-world Projects
  • Career Coaching Sessions
  • Email Support
Upto 60% OFF
Know More
Still have some questions? Let's discuss.
CONTACT US
Accept cookies & close this