what is IEnumerable and why we use this . both query return same result so why we use IEnumerable
If you already know the output type of linq query then you can use IEnumerable. if you take the sample code you provided you are using IEnumerable<int> in which you already provided int as the return type.
In certain cases if you are not sure on what the output type will be then we will use var.
Member
239 Points
1074 Posts
IEnumerable in linq
Jun 02, 2016 10:21 AM|Capricon User|LINK
i have two queries one is
and the other is
now anybody tell me what is IEnumerable and why we use this . both query return same result so why we use IEnumerable
All-Star
50741 Points
9877 Posts
Re: IEnumerable in linq
Jun 02, 2016 01:26 PM|A2H|LINK
If you already know the output type of linq query then you can use IEnumerable. if you take the sample code you provided you are using IEnumerable<int> in which you already provided int as the return type.
In certain cases if you are not sure on what the output type will be then we will use var.
Additional references
Aje
My Blog | Dotnet Funda
Member
239 Points
1074 Posts
Re: IEnumerable in linq
Jun 03, 2016 05:13 AM|Capricon User|LINK
what is the difference? both are same?
Member
670 Points
25 Posts
Re: IEnumerable in linq
Jun 03, 2016 05:45 AM|jignesht24|LINK
Hi,
When you use "var" compiler determines the type.
you can get the type using GetType() method.
For your second query, return type is "System.Linq.Enumerable".
also It would be good specify the return type when the type is already known.
And keyword var and IEnumerable is not same because var can be any type which is decide by compiler at design time.
hope this will help you.
Contributor
2570 Points
501 Posts
Re: IEnumerable in linq
Jun 03, 2016 05:56 AM|Cole Wu|LINK
Hi,
A var variable must be initialized at the time of declaration. but IEnumerable can do it: you could declare a IEnumerable variable like this:
but you couldn't declare a var variable like this:
Best regards,
Cole Wu