Click here to Skip to main content
15,918,742 members
Home / Discussions / C#
   

C#

 
GeneralRe: Best way to provide controls to plugins Pin
DaveyM6926-Jan-08 13:08
professionalDaveyM6926-Jan-08 13:08 
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 
Scott Dorman wrote:
1: Leave your class X defined as is, and change the call in the foreach loop to read
foreach (int n in (IEnumerable<int>x)
{
   System.Console.WriteLine(n);
}

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.

Scott Dorman wrote:
2: Leave the foreach call as is, and change X to read:

Both these work but there are still some oddities.

The following code compiles but throws a cast exception at runtime, which is what one (or at least I) would expect:
class X 
{
    private List<int> mList = new List<int>() ;
    public X() { mList.Add(1) ; mList.Add(2) ; mList.Add(3) ; }
    public IEnumerator GetEnumerator() { return mList.GetEnumerator() ; }
}

foreach ( X n in x ) // nb: iterate using an X, not an int
    System.Console.WriteLine( n ) ;


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() Roll eyes | :rolleyes: I don't particularly want to but it annoys me that I have to define this method but it never gets used Smile | :)

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 Smile | :) 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.

Oh well, I guess we can just chalk this one up to C# evolving on the fly. Thanks for your help.




I enjoy occasionally wandering around randomly, and often find that when I do so, I get to where I wanted to be [^].

Awasu 2.3.2 [^]: A free RSS/Atom feed reader with support for Code Project.

GeneralRe: What's the point of IEnumerable and IEnumerable&lt;T&gt;? Pin
Scott Dorman30-Jan-08 3:02
professionalScott Dorman30-Jan-08 3:02 
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 

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.