更新时间:2022-02-10 14:50:38
public static ReadOnlyCollection<T> AsReadOnlyCollection<T>(this IEnumerable<T> source)
{
if(source == null)
throw new ArgumentNulLException("source");
IList<T> list = source as IList<T> ?? source.ToList();
return new ReadOnlyCollection<T>(list);
}
请注意,在这种情况下,没有转换" IEnumerable<T>
之类的事情(与LINQ堆栈中的所有其他方法一样),您将获得与以前不同的对象.
Note that there is no such thing as "converting" an IEnumerable<T>
in this case (as with all other methods in the LINQ stack), you will get back a different object than before.