Click here to Skip to main content
15,886,551 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
public IEnumerable<KeyValuePair<int, string>> GetCity()
{
IEnumerable<KeyValuePair<int, string>> CityList = new List<KeyValuePair<int, string>>();
IEnumerable<City_Mast> objCity = ent.City_Mast.ToList();
CityList = objCity.Select(f => new KeyValuePair<int, string>(f.cid, f.city));
return CityList;
}

 public List<KeyValuePair<int, string>> GetCity()
{
List<KeyValuePair<int, string>> CityList = new List<KeyValuePair<int, string>>();
List<City_Mast> objCity = ent.City_Mast.ToList();
CityList = objCity.Select(f => new KeyValuePair<int, string>(f.cid, f.city)).ToList();
return CityList;
}


I Used IEnumerable and List Both in Above Example.
Can Anyone tell me which one is faster?
when to use IEnumerable and list?
Posted
Updated 10-Aug-18 12:36pm
Comments
Milfje 5-Jun-15 5:45am    
Isn't a List just an implementation of IList which in turn is an implementation of IEnumerable? And why use a List with a KeyValuePair? Wouldn't it be easier to use a Dictionary?

Here's some info and a comparison between collections: Link
OriginalGriff 5-Jun-15 5:54am    
Not necessarily: a Dictionary is a specific implementation of a KVP which does not allow duplicate key values: A List of KVP objects can have identical keys.
Milfje 5-Jun-15 6:00am    
Every day is a school day. Thanks, didn't know that :)
OriginalGriff 5-Jun-15 6:04am    
You're welcome!
Mayur Ahir 5-Jun-15 5:52am    
Actually i am new in Interface topic.
i just ask difference between them.
can you tell me implementation of both?

A List is an IEnumerable, via an intermediary - if you browse the Reference Source:
C#
public class List<T> : IList<T>, System.Collections.IList, IReadOnlyList<T>

And a IList is
C#
public interface IList<T> : ICollection<T>
And:
C#
public interface ICollection<T> : IEnumerable<T>

So in effect they are the same when it comes to actually using them, and speed difference will depend on exactly what you are doing.

In the case of your code, the first one will be significantly faster, because it doesn;t execute the Selects - it just sets it up for later execution. Unlike your List code which does execute the selects because the use of ToList "collapses the operation" and actually requires that the collection is iterated.

If you want to know what is fastest, then test it!
There is a Stopwatch class in System.Diagnostics for just that purpose.
 
Share this answer
 
It depends what your calling code does. IEnumerable is a one-shot, forward-only mechanism, whereas a List is in memory and can be freely navigated as much as needed. If the calling code runs through every city once (ie showing the values in a drop down) then it's neither here nor there. If only one, or some cities are shown then IEnumerable is better, if the cities are ran through multiple times, or re-ordered, or have you doing multiple selects\filters etc then the List is better.
 
Share this answer
 
so many useless answers...

let me explain main issue.

1) if you iterate elements from IEnumerable or List only one time, so both are same, no differences.

2) if you iterate elements many times, or if you get an one element for multiple times, so, IEnumerable will be slow.

Because of IEnumerable it does not contain result, so that it creates an new instance of result when it's used. It means, when you access an same element multiple times, each will be different object!
 
Share this answer
 
Comments
Dave Kreskowiak 10-Aug-18 19:24pm    
Ummm... I hate to say this but yours is the useless answer, to a question that was asked and answered three years ago.

There is no speed difference because there is no code in an interface. The original question was like asking "how fast is the car compared to how fast is the engine bay in it?"

You have to compare implementations of the interface, not the interface itself.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900