Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

1.Difference between for and foreach loop S.

No 1 For loop Foreach loop

In case of for the variable of the loop is In case of Foreach the variable of the loop always be int only. while be same as the type of values under the array. The For loop executes the statement or block of statements repeatedly until specified expression evaluates to false. There is need to specify the loop bounds(Minimum, Maximum). example: using sytem; class class1 { static void Main() { int j=0; for(int i=0; i<=10;i++) { j=j+1; } Console.ReadLine(); } } The Foreach statement repeats a group of embedded statements for each element in an array or an object collection. We do not need to specify the loop bounds minimum or maximum. example: using sytem; class class1 { static void Main() { int j=0; int[] arr=new int[] {0,3,5,2,55,34,643,42,23}; foreach(int i in arr) { j=j+1; } Console.ReadLine(); } }

3 4

2. Difference between Covariance and Contravariance S.No 1 Covariance Converting from a broader type to a specific type is called co-variance.If B is derived from A and B relates to A, then we can assign A to B. Like A=B. This is Covariance. Contravariance Converting from a more specific type to a broader type is called contra-variance. If B is derived from A and B relates to A, then we can assign B to A. Like B= A. This is Contravariance.

Co-variance is guaranteed to work Contra-variance on the other hand is not without any loss of information during guaranteed to work without loss of data. As conversion. So, most languages also such an explicit cast is required. provide facility for implicit conversion. e.g. Converting from cat or dog to animal is e.g. Assuming dog and cat inherits from called contra-variance, because not all animal, when you convert from animal features (properties/methods) of cat or dog is type to dog or cat, it is called copresent in animal. variance.

Example:

class Fruit { } class Mango : Fruit { } class Program { delegate T Func<out T>(); delegate void Action<in T>(T a); static void Main(string[] args) { // Covariance Func<Mango> mango = () => new Mango(); Func<Fruit> fruit = mango; // Contravariance Action<Fruit> fr = (frt) => { Console.WriteLine(frt); }; Action<Mango> man = fr; } } 4 Note: 1. Co-variance and contra-variance is possible only with reference types; value types are invariant. 2. In .NET 4.0, the support for co-variance and contra-variance has been extended to generic types. No now we can apply co-variance and contra-variance to Lists etc. (e.g. IEnumerable etc.) that implement a common interface. This was not possible with .NET versions 3.5 and earlier.

3.Difference between IList and IEnumerable

S.No 1

IList IList is used to access an element in a specific position/index in a list. IList is useful when we want to Add or remove items from the list. IList can find out the no of elements in the collection without iterating the collection. IList does not support filtering.

IEnumerable IEnumerable is a forward only collection, it can not move backward and between the items. IEnumerable does not support add or remove items from the list. Using IEnumerable we can find out the no of elements in the collection after iterating the collection. IEnumerable supports filtering.

2 3

4.Difference between IEnumerable and IQueryable

S.No 1 2

IEnumerable IEnumerable exists in System.Collections Namespace. IEnumerable is best to query data from in-memory collections like List, Array etc. While query data from database, IEnumerable execute select query on server side, load data in-memory on client side and then filter data. IEnumerable is suitable for LINQ to Object and LINQ to XML queries. IEnumerable does not supports custom query. IEnumerable does not support lazy loading. Hence not suitable for paging like scenarios. Extension methods supports by IEnumerable takes functional objects. IEnumerable Example

IQueryable IQueryable exists in System.Linq Namespace. IQueryable is best to query data from outmemory (like remote database, service) collections. While query data from database, IEnumerable execute select query on server side with all filters. IQueryable is suitable for LINQ to SQL queries. IQueryable supports custom query using CreateQuery and Execute methods. IQueryable support lazy loading. Hence it is suitable for paging like scenarios. Extension methods supports by IEnumerable takes expression objects means expression tree.

4 5 6

MyDataContext dc = new MyDataContext (); IEnumerable list = dc.loyees.Where(p => p.Name.StartsWith("S")); list = list.Take(10); Generated SQL statements of above query will be : SELECT [t0].[EmpID], [t0].[EmpName], [t0].[Salary] FROM [Employee] AS [t0] WHERE [t0].[EmpName] LIKE @p0

Note: In this query "top 10" is missing since IEnumerable filters records on client side IQueryable Example MyDataContext dc = new MyDataContext (); IEnumerable list = dc.loyees.Where(p => p.Name.StartsWith("S")); list = list.Take(10); Generated SQL statements of above query will be : SELECT TOP 10 [t0].[EmpID], [t0].[EmpName], [t0].[Salary] FROM [Employee] AS [t0] WHERE [t0].[EmpName] LIKE @p0 Note: In this query "top 10" is exist since IQueryable executes query in SQL server with all filters. 5.Difference between IEnumerable and IEnumerator S.No 1 IEnumerable The IEnumerable interface is a generic interface that provides an abstraction for looping over elements. In addition to providing foreach support, it allows us to tap into the useful extension methods in the System.Linq namespace, opening up a lot of advanced functionality The IEnumerable interface contains an abstract member function called GetEnumerator() and return an interface IEnumerator on any success call. IEnumerable does not remember the cursor state i.e currently row which is iterating through IEnumerable is useful when we have only iterate the value IEnumerator IEnumerator provides two abstract methods and a property to pull a particular element in a collection. And they are Reset(), MoveNext() and Current The signature of IEnumerator members is as follows: void Reset() : Sets the enumerator to its initial position, which is before the first element in the collection. bool MoveNext() : Advances the enumerator to the next element of the collection. object Current : Gets the current element in the collection IEnumerator does remember the cursor state

IEnumerator is useful when we have to pass the iterator as parameter and has to remember the value

You might also like