c# - List GetEnumerator return type does not match interface -


in visual studio 2015, when go list.cs , see class declaration, see 1 method named getenumerator:

public enumerator getenumerator(); 

on other hand, interface ienumerable<t> , ienumerable specifies must define method format is:

ienumerator<t> getenumerator(); ienumerator getenumerator(); 

this made me believe interface method can implemented though reuturn type not excatly match. although, turned out wrong when tried.

what happening here?

if @ source see 2 different getenumerator methods:

public enumerator getenumerator() {     return new enumerator(this); }  ienumerator<t> ienumerable<t>.getenumerator() {     return new enumerator(this); } 

the second 1 used if accessing object through ienumerator interface. both use same underlying implementation however.

[serializable] public struct enumerator : ienumerator<t>, system.collections.ienumerator {   ... } 

you can still typed version follows:

var examplelist = new list<string>(); var enumerator1 = ((ienumerable<string>)examplelist).getenumerator() var enumerator2 = (ienumerable<string>) examplelist.getenumerator() 

for enumerator1 casting list use getenumerator() method ienumerable interface. enumerator2 relying on fact same implementation used in in each , therefore can cast enumerator.


Comments

Popular posts from this blog

html - Styling progress bar with inline style -

java - Oracle Sql developer error: could not install some modules -

How to use autoclose brackets in Jupyter notebook? -