Search code examples
c#.netarraysilist

Does casting an IList<T> to an array of type T[] cause enumeration?


I have a method that looks like:

T[] field;

public Method(IList<T> argument)
{
    this.field = (T[])argument;
}

When the body of the method is executed does enumeration take place during the cast? Would that change if the underlying type was different?


Solution

  • No, it won't enumerate anything. It will either succeed if argument actually is a T[], or throw an InvalidCastException exception if it isn't. (Or return null if argument is null.)