Click here to Skip to main content
15,897,518 members
Home / Discussions / C#
   

C#

 
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 
GeneralRe: What's the point of IEnumerable and IEnumerable<T>? [modified] Pin
Taka Muraoka30-Jan-08 4:23
Taka Muraoka30-Jan-08 4:23 
GeneralRe: What's the point of IEnumerable and IEnumerable<T>? Pin
Scott Dorman30-Jan-08 14:56
professionalScott Dorman30-Jan-08 14:56 
GeneralRe: What's the point of IEnumerable and IEnumerable<T>? Pin
Taka Muraoka30-Jan-08 21:46
Taka Muraoka30-Jan-08 21:46 
Scott Dorman wrote:
Defining the member in an interface doesn't imply that it is virtual at all.


Aha! This is the root of my confusion. I was assuming that a C# interface was the same as a C++ interface (i.e. a collection of pure virtual/abstract methods). I remembered reading in the O'Reilly "C# Essentials" book that a C# interface was simply a syntactic shortcut for a bunch of abstract members in an abstract class but their exact wording was "similar to" Hmmm | :|

The output from the following code gave me a surprise:
interface I
{
    void foo( string msg ) ;
}

class A : I
{
    public void foo( string msg ) { System.Console.WriteLine( msg+" -> A::foo" ) ; }
}

class B : A
{
    public void foo( string msg ) { System.Console.WriteLine( msg+" -> B::foo" ) ; }
}

static void Main()
{
    B b = new B() ;
    ((I)b).foo( "Static type I" ) ;
    ((A)b).foo( "Static type A" ) ;
    ((B)b).foo( "Static type B" ) ;
}

--- OUTPUT ---
Static type I -> A::foo
Static type A -> A::foo
Static type B -> B::foo

In C++, the B::foo() method would've been called every time.

No wonder I thought foreach appeared to be ignoring the IEnumerable's in the inheritance hierarchy. In every other OO language I've worked with, to get access to GetEnumerator() you would have to go through the IEnumerable but in C#, deriving from an interface seems to be little more than a directive to the compiler telling it that certain methods need to defined i.e. it doesn't really affect the class as such (e.g. by causing the layout of the v-table to change, or whatever C# uses).

Playing around a little with the definition of B:
class B : A,I
{
    void foo( string msg ) { System.Console.WriteLine( msg+" -> B::foo" ) ; }
}

--- OUTPUT ---
Static type I -> A::foo
Static type A -> A::foo
Static type B -> A::foo

That's weird, but I think because B::foo() is not public:
class B : A,I
{
    public void foo( string msg ) { System.Console.WriteLine( msg+" -> B::foo" ) ; }
}

--- OUTPUT ---
Static type I -> B::foo
Static type A -> A::foo
Static type B -> B::foo

And how about these two:
class B : A,I
{
    void foo( string msg ) { System.Console.WriteLine( msg+" -> B::foo" ) ; }
    void I.foo( string msg ) { System.Console.WriteLine( msg+" -> B::foo 2" ) ; }
}

--- OUTPUT ---
Static type I -> B::foo 2
Static type A -> A::foo
Static type B -> A::foo

class B : A,I
{
    public void foo( string msg ) { System.Console.WriteLine( msg+" -> B::foo" ) ; }
    void I.foo( string msg ) { System.Console.WriteLine( msg+" -> B::foo 2" ) ; }
}

--- OUTPUT ---
Static type I -> B::foo 2
Static type A -> A::foo
Static type B -> B::foo

I think I get it Unsure | :~

Scott Dorman wrote:
I am explicitly implementing the IEnumerable.GetEnumerator member. In the calling code, it will default to using the implicit implementation (the public IEnumerator<int> GetEnumerator()) because the call isn't being cast to IEnumerable.


Ah, so by writing IEnumerator IEnumerable.GetEnumerator() { ... } you are saying that this method is only available if your static type (i.e. at compile time) is IEnumerable. I was going to say this seems to break polymorphism but that doesn't apply if the method is not virtual.

Scott Dorman wrote:
hopefuly my answers above make it clearer as to why declaring one of the methods public makes a difference.


This I still don't quite get. You were refering to the two GetEnumerator methods but something still seems intuitively wrong: they are implicitly public, because that's how they are defined in their respective interfaces, so explicitly declaring one as public should be redundant but somehow, it actually changes how things work WTF | :WTF: It certainly did when I was playing around with the definition of B above.

But I'm heartily sick of this, as I'm sure you are. Thanks again for the help Smile | :)




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.

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 
GeneralRe: String Reversal plus character replacement Pin
PIEBALDconsult25-Jan-08 13:21
mvePIEBALDconsult25-Jan-08 13:21 
GeneralRe: String Reversal plus character replacement Pin
BREdwards25-Jan-08 17:31
BREdwards25-Jan-08 17:31 
GeneralRe: String Reversal plus character replacement Pin
DaveyM6925-Jan-08 22:00
professionalDaveyM6925-Jan-08 22:00 

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.