- i have generic class a<t>, implements ienumerable<t[]>.
i want have convenience wrapper b inherits a<char> , implements ienumerable<string>.
public class a<t> : ienumerable<t[]> { public ienumerator<t[]> getenumerator() { return enumerate().getenumerator(); } ienumerator ienumerable.getenumerator() { return getenumerator(); } protected ienumerable<t[]> enumerate() { throw new system.notimplementedexception(); } } public class b : a<char>, ienumerable<string> { public ienumerator<string> getenumerator() { return enumerate().select(s => new string(s)).getenumerator(); } ienumerator ienumerable.getenumerator() { return getenumerator(); } }
now, works fine, foreach variable type inferred string:
b b = new b(); foreach (var s in b) { string[] split = s.split(' '); }
but won't compile, saying "the type arguments cannot inferred usage, try specifying type arguments explicitly":
string[] strings = b.toarray();
however, works:
string[] strings = b.toarray<string>();
can explain compiler behavior?
obviously, b implements both ienumerable<char[]> , ienumerable<string> , can't figure out of them want call, why works fine in "foreach" sample?
please, don't suggest me solve problem composition - last resort me.
the difference following:
foreach
looks public method called getenumerator
. doesn't care ienumerable<t>
. class b
has 1 public method named getenumerator
: 1 defined in b
hides 1 defined in a
.
toarray
on other hand extension method on ienumerable<t>
. class both ienumerable<string>
, ienumerable<char[]>
call ambiguous between 2 generic arguments string
, char[]
.
Comments
Post a Comment