1.Difference
between for and foreach loop
S.No | For loop | Foreach loop |
1 | In case of for the variable of the loop is always be int only. | In case of Foreach the variable of the loop while be same as the type of values under the array. |
2 | The For loop executes the statement or block of statements repeatedly until specified expression evaluates to false. | The Foreach statement repeats a group of embedded statements for each element in an array or an object collection. |
3 | There is need to specify the loop bounds(Minimum, Maximum). | We do not need to specify the loop bounds minimum or maximum. |
4 |
example:
using sytem; class class1 { static void Main() { int j=0; for(int i=0; i<=10;i++) { j=j+1; } Console.ReadLine(); } } |
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(); } } |
2.
Difference between Covariance and Contravariance
S.No | Covariance | Contravariance |
1 | 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. | 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. |
2 | Co-variance is
guaranteed to work without any loss of information during
conversion. So, most languages also provide facility for implicit
conversion. e.g. Assuming dog and cat inherits from animal, when you convert from animal type to dog or cat, it is called co-variance. |
Contra-variance
on the other hand is not guaranteed to work without loss of data.
As such an explicit cast is required. e.g. Converting from cat or dog to animal is called contra-variance, because not all features (properties/methods) of cat or dog is present in animal. |
3 |
Example: class Fruit { } class Mango : Fruit { } class Program { delegate T Func delegate void Action static void Main(string[] args) { // Covariance Func Func // Contravariance Action { Console.WriteLine(frt); }; Action } } |
|
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 | IList | IEnumerable |
1 | IList is used to access an element in a specific position/index in a list. | IEnumerable is a forward only collection, it can not move backward and between the items. |
2 | IList is useful when we want to Add or remove items from the list. | IEnumerable does not support add or remove items from the list. |
3 | IList can find out the no of elements in the collection without iterating the collection. | Using IEnumerable we can find out the no of elements in the collection after iterating the collection. |
4 | IList does not support filtering. | IEnumerable supports filtering. |
4.Difference
between IEnumerable and IQueryable
S.No | IEnumerable | IQueryable |
1 | IEnumerable exists in System.Collections Namespace. | IQueryable exists in System.Linq Namespace. |
2 | IEnumerable is best to query data from in-memory collections like List, Array etc. | IQueryable is best to query data from out-memory (like remote database, service) collections. |
3 | While query data from database, IEnumerable execute select query on server side, load data in-memory on client side and then filter data. | While query data from database, IEnumerable execute select query on server side with all filters. |
4 | IEnumerable is suitable for LINQ to Object and LINQ to XML queries. | IQueryable is suitable for LINQ to SQL queries. |
5 | IEnumerable does not supports custom query. | IQueryable supports custom query using CreateQuery and Execute methods. |
6 | IEnumerable does not support lazy loading. Hence not suitable for paging like scenarios. | IQueryable support lazy loading. Hence it is suitable for paging like scenarios. |
7 | Extension methods supports by IEnumerable takes functional objects. | Extension methods supports by IEnumerable takes expression objects means expression tree. |
8 | IEnumerable
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 [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 | IEnumerable | IEnumerator |
1 |
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. |
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 |
2 |
IEnumerable
does not remember the cursor state i.e currently row which is
iterating through
|
IEnumerator
does remember the cursor state
|
3 |
IEnumerable
is useful when we have only iterate the value
|
IEnumerator
is useful when we have to pass the iterator as parameter and has
to remember the value
|
This is very interesting information. Please keep updating
ReplyDeleteThanks.Will post more like this.
ReplyDelete
ReplyDeleteThis type of information are easy to explains more about Dot net Programming Concepts.It helspful to learn more
Best Dot Net Training Institutes in Chennai
ReplyDeleteHowdy, would you mind letting me know which web host you’re utilizing? I’ve loaded your blog in 3 completely different web browsers, and I must say this blog loads a lot quicker than most. Can you suggest a good internet hosting provider at a reasonable price?
Best AWS Amazon Web Services Training in Chennai | AWS Training in Chennai cost
Amazon Web Services Training in Tambaram, Chennai|Best AWS Training in Tambaram, Chennai
I like your blog. Thanks for Sharing the good post
ReplyDelete.NET Training in Delhi
nice course. thanks for sharing this post.
ReplyDeleteDot Net Training in Noida