Click here to Skip to main content
15,908,776 members
Home / Discussions / C#
   

C#

 
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 
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 
Taka Muraoka wrote:
I haven't really bothered looking too deeply into IL but as a hard-core C++ guy, it certainly doesn't make any sense.


The IL is much closer to reading assembly than anything else, but it can sometimes reveal some interesting results.

Taka Muraoka wrote:
Well, it doesn't really make any sense from a general OO point of view, either


I can't disagree with this. I was only saying why I think it might have been done that way.

Taka Muraoka wrote:
Just stepping through in the debugger. Modifying it in the code sample in my OP to return null seems to have no ill effects.


What you are seeing is actually correct behavior for your code. You have explicitly defined the IEnumerable<T>.GetEnumerator() and implicitly defining the IEnumerable.GetEnumerator(). Since IEnumerable.GetEnumerator() is public (and implicit) this code gets called. There are a few ways to change this so it calls the generic enumerator:

1: Leave your class X defined as is, and change the call in the foreach loop to read
C#
foreach (int n in (IEnumerable<int>)x)
{
   System.Console.WriteLine(n);
}
2: Leave the foreach call as is, and change X to read:
C#
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();
    }
 
   IEnumerator IEnumerable.GetEnumerator()
    {
        return mList.GetEnumerator();
    }
}
or
C#
class X : IEnumerable<int>
{
    private List<int> mList = new List<int>();
    public X()
    {
        mList.Add(1); mList.Add(2); mList.Add(3);
    }
 
    public IEnumerator<int> GetEnumerator()
    {
        return mList.GetEnumerator();
    }
 
   IEnumerator IEnumerable.GetEnumerator()
    {
        return mList.GetEnumerator();
    }
}


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;? 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 
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 

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.