Instantiating an IEnumerable array in C# -
i wondering why can in c#:
ienumerable<int>[] nums = new ienumerable<int>[10];
but cannot this:
ienumerable<int> nums = new ienumerable<int>();
what c# doing under hood first statement? thought couldn't create instances of interfaces new keyword.
the first statement creating new array of size 10 of item type ienumerable<int>
. array concrete type can create.
to set item in array, this:
num[0] = new list<int>() {1,2,3};
although item type ienumerable<int>
, cannot create instance of ienumerable<int>
. have create instance of class implements ienumerable<int>
list<int>
.
in second example, try create instance of ienumerable<int>
interface, i.e. not class, , not compile.
the variable type can still ienumerable<int>
, have create instance of class implements ienumerable<int>
this:
ienumerable<int> nums = new list<int>() {1,2,3};
Comments
Post a Comment