Click here to Skip to main content
16,015,665 members
Home / Discussions / C#
   

C#

 
QuestionWhat's the point of IEnumerable and IEnumerable<T>? Pin
Taka Muraoka25-Jan-08 16:30
Taka Muraoka25-Jan-08 16:30 
AnswerRe: What's the point of IEnumerable and IEnumerable<T>? Pin
Scott Dorman28-Jan-08 18:19
professionalScott Dorman28-Jan-08 18:19 
GeneralRe: What's the point of IEnumerable and IEnumerable<T>? Pin
Taka Muraoka28-Jan-08 18:40
Taka Muraoka28-Jan-08 18:40 
GeneralRe: What's the point of IEnumerable and IEnumerable<T>? Pin
Scott Dorman29-Jan-08 4:33
professionalScott Dorman29-Jan-08 4:33 
GeneralRe: What's the point of IEnumerable and IEnumerable<T>? Pin
Taka Muraoka29-Jan-08 4:47
Taka Muraoka29-Jan-08 4:47 
GeneralRe: What's the point of IEnumerable and IEnumerable<T>? Pin
Scott Dorman29-Jan-08 5:43
professionalScott Dorman29-Jan-08 5:43 
GeneralRe: What's the point of IEnumerable and IEnumerable<T>? Pin
Taka Muraoka29-Jan-08 15:46
Taka Muraoka29-Jan-08 15:46 
GeneralRe: What's the point of IEnumerable and IEnumerable<T>? Pin
Scott Dorman30-Jan-08 3:02
professionalScott Dorman30-Jan-08 3:02 
Taka Muraoka wrote:
This is obviously kinda ugly. The intent of deriving from IEnumerable<t> is to enforce type-safety and I was hoping that the compiler would be smart enough to realize that X can only be iterated over using int's and nothing else.


I can't disagree with you here. I pointed this out as an option...I never said it was a good one.

Taka Muraoka wrote:
Both these work but there are still some oddities.


What are the oddities?

Taka Muraoka wrote:
But using the modified foreach with either of your #2 code samples doesn't even compile ("Cannot convert int to X"). So it seems that there's now no way to get into the non-generic GetEnumerator()


That is correct. By deriving X from IEnumerable<int> you are effectively telling the type system that when you enumerate over X (by calling GetEnumerator) that you are returning a type of int not X. Isn't that the point of providing the strongly typed version? You (and the compiler) know at compile time what the data type will be, so there is no need for it to call the non-typed version.

Taka Muraoka wrote:
I don't particularly want to but it annoys me that I have to define this method but it never gets used


True, but that's how interfaces work. If you derive from an interface you have to implement all of the methods defined in the contract.

Taka Muraoka wrote:
So, it looks like foreach is ignoring whether the object it's iterating over derives from IEnumerable or IEnumerable<T> and just looks for a public GetEnumerator() method (which is kinda lame). But then how does it know which GetEnumerator() to use in your 2a code? Both of them are public in their respective interfaces, and yes, I tried reversing the order they are defined in I was kinda hoping the compiler could infer which one to use by the type being used in the foreach statement but for some reason, it's only allowing the generic one to be used.


I'm not sure I would say that it's ignoring the interfaces. It knows to use the generic GetEnumerator in the 2a example based on the type defined in the foreach loop. The key is which method is defined explicitly or implicitly. In my 2a example, they are both explicitly defined (notice there is not a public keyword). These methods aren't available unless the object is explicitly cast to the correct interface, which the compiler is being smart enough to do for you based on the underlying type of the List<T>. When you declare one of them as public you are telling the compiler that this is the one you want to use by default.

Scott.

—In just two days, tomorrow will be yesterday.
—Hey, hey, hey. Don't be mean. We don't have to be mean because, remember, no matter where you go, there you are. - Buckaroo Banzai

[Forum Guidelines] [Articles] [Blog]

GeneralRe: What's the point of IEnumerable and IEnumerable&lt;T&gt;? [modified] Pin
Taka Muraoka30-Jan-08 4:23
Taka Muraoka30-Jan-08 4:23 
GeneralRe: What's the point of IEnumerable and IEnumerable&lt;T&gt;? Pin
Scott Dorman30-Jan-08 14:56
professionalScott Dorman30-Jan-08 14:56 
GeneralRe: What's the point of IEnumerable and IEnumerable&lt;T&gt;? Pin
Taka Muraoka30-Jan-08 21:46
Taka Muraoka30-Jan-08 21:46 
Generalclass Pin
keisal25-Jan-08 13:57
keisal25-Jan-08 13:57 
GeneralRe: class Pin
Luc Pattyn25-Jan-08 15:51
sitebuilderLuc Pattyn25-Jan-08 15:51 
GeneralRe: class Pin
Paul Conrad25-Jan-08 15:52
professionalPaul Conrad25-Jan-08 15:52 
GeneralRe: class Pin
BoneSoft25-Jan-08 18:55
BoneSoft25-Jan-08 18:55 
GeneralRe: class Pin
DaveyM6925-Jan-08 21:29
professionalDaveyM6925-Jan-08 21:29 
GeneralRe: class Pin
Spacix One26-Jan-08 5:57
Spacix One26-Jan-08 5:57 
AnswerRe: class Pin
Guffa26-Jan-08 8:16
Guffa26-Jan-08 8:16 
GeneralGeneric List Remove method delay Pin
DaveyM6925-Jan-08 12:35
professionalDaveyM6925-Jan-08 12:35 
GeneralRe: Generic List Remove method delay Pin
Abhijit Jana25-Jan-08 23:27
professionalAbhijit Jana25-Jan-08 23:27 
GeneralRe: Generic List Remove method delay Pin
DaveyM6925-Jan-08 23:31
professionalDaveyM6925-Jan-08 23:31 
GeneralString Reversal plus character replacement Pin
BREdwards25-Jan-08 11:25
BREdwards25-Jan-08 11:25 
GeneralRe: String Reversal plus character replacement Pin
pmarfleet25-Jan-08 11:54
pmarfleet25-Jan-08 11:54 
GeneralRe: String Reversal plus character replacement Pin
BREdwards25-Jan-08 12:16
BREdwards25-Jan-08 12:16 
GeneralRe: String Reversal plus character replacement Pin
pmarfleet25-Jan-08 12:25
pmarfleet25-Jan-08 12:25 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.