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

C#

 
GeneralRe: Windows right click menu... Pin
Abhijit Jana25-Jan-08 23:24
professionalAbhijit Jana25-Jan-08 23:24 
GeneralRe: Windows right click menu... Pin
DaveyM6925-Jan-08 23:29
professionalDaveyM6925-Jan-08 23:29 
GeneralRe: Windows right click menu... Pin
Spacix One26-Jan-08 6:03
Spacix One26-Jan-08 6:03 
GeneralWhere to find everything about Intellisence in VS 2008 Pin
anderslundsgard25-Jan-08 22:42
anderslundsgard25-Jan-08 22:42 
GeneralRe: Where to find everything about Intellisence in VS 2008 Pin
pmarfleet26-Jan-08 5:32
pmarfleet26-Jan-08 5:32 
GeneralBest way to provide controls to plugins Pin
rotsey25-Jan-08 22:16
rotsey25-Jan-08 22:16 
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 
So, I found out by accident that a class doesn't actually need to implement IEnumerable to be able to foreach over it.

This works just fine (VS2005):
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() ; }
}

X x = new X() ;
foreach ( int n in x )
    System.Console.WriteLine( n ) ;

MSDN confirms[^] this behaviour:
Evaluates to a type that implements IEnumerable or a type that declares a GetEnumerator method.

Although this[^] suggests that it's a C# thing only.
The reason for implementing IEnumurable therefore, is (a) to identify the class as enumerable/foreach-able, and (b) to provide a language independant implementation that will work in other languages that don't provide the C# foreach performance "hack".
But even given that, it still begs the question: why did the C# team feel the need for foreach to accept objects that don't implement IEnumerable?

But it gets worse when using generics. Deriving X from IEnumerable<T> forces you to implement both the generic (expected) and non-generic (huh?!) version of GetEnumerator():
class X : IEnumerable<int>
{
    private List<int> mList = new List<int>() ;
    public X() { mList.Add(1) ; mList.Add(2) ; mList.Add(3) ; }
    IEnumerator<int> IEnumerable<int>.GetEnumerator() { return mList.GetEnumerator() ; }
    public IEnumerator GetEnumerator() { return mList.GetEnumerator() ; }
}

X x = new X() ;
foreach ( int n in x )
    System.Console.WriteLine( n ) ;
Except that when stepping through the debugger, foreach uses the *non-generic* GetEnumerator(). So why even bother having the generic one?

I'm pretty new to C# but this seems kinda dumb. Am I missing something?




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.

AnswerRe: What's the point of IEnumerable and IEnumerable&lt;T&gt;? Pin
Scott Dorman28-Jan-08 18:19
professionalScott Dorman28-Jan-08 18:19 
GeneralRe: What's the point of IEnumerable and IEnumerable&lt;T&gt;? Pin
Taka Muraoka28-Jan-08 18:40
Taka Muraoka28-Jan-08 18:40 
GeneralRe: What's the point of IEnumerable and IEnumerable&lt;T&gt;? Pin
Scott Dorman29-Jan-08 4:33
professionalScott Dorman29-Jan-08 4:33 
GeneralRe: What's the point of IEnumerable and IEnumerable&lt;T&gt;? Pin
Taka Muraoka29-Jan-08 4:47
Taka Muraoka29-Jan-08 4:47 
GeneralRe: What's the point of IEnumerable and IEnumerable&lt;T&gt;? Pin
Scott Dorman29-Jan-08 5:43
professionalScott Dorman29-Jan-08 5:43 
GeneralRe: What's the point of IEnumerable and IEnumerable&lt;T&gt;? Pin
Taka Muraoka29-Jan-08 15:46
Taka Muraoka29-Jan-08 15:46 
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 

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.