将IList 投射到T []类型的数组是否会导致枚举?(Does casting an IList to an array of type T[] cause enumeration?)

我有一个看起来像这样的方法:

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

当方法的主体执行时,枚举是否在投射期间发生? 如果底层类型不同,会不会改变?

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?

最满意答案

不,它不会列举任何东西。 如果argument实际上一个T[] ,它将成功,如果它不是,则抛出一个InvalidCastException异常。 (如果argument为null,则返回null。)

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.)

将IList 投射到T []类型的数组是否会导致枚举?(Does casting an IList to an array of type T[] cause enumeration?)

我有一个看起来像这样的方法:

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

当方法的主体执行时,枚举是否在投射期间发生? 如果底层类型不同,会不会改变?

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?

最满意答案

不,它不会列举任何东西。 如果argument实际上一个T[] ,它将成功,如果它不是,则抛出一个InvalidCastException异常。 (如果argument为null,则返回null。)

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.)