"materialize"는 이와 같은 코드에 적합한 이름입니까, 아니면 더 좋은 (공식적인) 이름이 있습니까?열거 형 : 공식 용어를 "구체화"합니까?
enumerable as ICollection<T> ?? enumerable .ToArray()
편집 : 확장 방법으로
static void Save(IEnumerable<string> strings)
{
// The following code is Resharper suggested solution to
// "Possible multiple enumeration of IEnumerable" warning
// (http://confluence.jetbrains.com/display/ReSharper/Possible+multiple+enumeration+of+IEnumerable):
strings = strings as string[] ?? strings.ToArray(); // you're not calling
// ToArray because you
// need an array, here
if (strings.Any(s => s.Length >= 255)) throw new ArgumentException();
File.AppendAllLines("my.path.txt", strings);
}
첫번째 라인 더 서술 될한다 :
I 문제가// or "MaterializeIfNecessary"
public static IEnumerable<T> Materialize<T>(this IEnumerable<T> source)
{
// if you use code analysis tools like resharper, you may have to return a
// different type to turn off warnings - even a placeholder interface like
// IMaterializedEnumerable<T> : IEnumerable<T> { }
if (source == null) return null;
return source as ICollection<T> ?? source.ToArray();
}
코드 (및 목적) 명확히
strings = strings.MaterializeIfNecessary();
내가 전화 것'AsReadOnly' 또는'ToReadOnlyCollection' – Magnus
@Magnus는 대답 : BTW 내가 생각해야하는'AsReadOnlyCollection'는 –
가 왜 IReadOnlyCollection에 IEnumerable을 던졌다 더 나은 결과를 설명? http://stackoverflow.com/a/55661/211627 – JDB