Posts

Showing posts from May, 2024

C# | IList Vs IEnumerable

In C#, the choice between IEnumerable, List, and IList depends on your specific use case and requirements. Each of these types serves a different purpose, and the choice should be based on your performance and optimization needs. Let's discuss when to use each and their implications in terms of time and space complexity IEnumerable Use IEnumerable when you want to work with a sequence of elements without any specific order or when you want to enable deferred execution. It provides a forward-only, read-only cursor over a collection. Example IEnumerable < int > numbers = new List < int > { 1 , 2 , 3 , 4 , 5 } ; foreach ( int number in numbers ) { Console . WriteLine ( number ) ; } C# Copy Performance and Optimization IEnumerable is suitable when you don't need to modify the collection, and you can leverage deferred execution to optimize your operations. It may be more memory-efficient because it doesn't store the entire sequence in memory. Th...

C# | IEnumerable Vs IQueryable In LINQ

Image
IEnumerable and IQueryable are used for data manipulation in LINQ from the database and collections. For getting the data from the database, I have created a table named "Employee" that has some data and looks like: Then creating the Data Context class (.dbml class) in your project that converts the database table named "Employee" as a class. Now I will tell you the functionality and some basic differences between IEnumerable and IQueryable using the object of the Data Context class.. IEnumerable Code SQL statement after execution of above query After the execution of line number 18, the SQL statement will look like the following until the end: IQueryable Code SQL statement after execution of the preceding query After the execution of line number 22, the SQL statement will look like: But after the execution of line number 23, SQL statement will add the Top for the filtering. In both syntaxes I am accessing the data from the Employee table and then taking only 3 rows...

C# | IList Vs IEnumerable Vs ICollection

Generally, developers are confused about when and why should they use IEnumerable OR ICollection OR IList Or List. So, in this article, I am exploring more when and why to use each. When and why we should choose IEnumerable This is the base interface for any collection under system.Collection. If you want iteration only, then it is the best choice to use. Iteration means that you do not want any index based search or any write or any insert or remove.(only read records). You can only read records, nothing else. public   interface  IEnumerable {        // Summary:         // Returns an enumerator that iterates through a collection.         //         // Returns:         // An System.Collections.IEnumerator object that can be used to...