LINQ vs LSP

防衛的プログラミングに関する以前の投稿に応えて読者の一人がこの質問を送ってきました。

[1]非常によく知られている防御プログラミングのシナリオは、入力パラメーターが
 IEnumerable 
      



public class Publisher { public Publisher(IEnumerable<Subscriber> subscribers) { // defensive copy -> good or bad? this.subscribers = subscribers.ToArray(); } // … }








« : IEnumerable, , . , ICollection



, ( ..: , . , , . ICollection



, , ? - .
)
. , , 'publisher' (, ). , , , . , , .

« , , -, - , IEnumerable



. , , – .

« , , – ( ), .







.



.

IEnumerable .NET. , , , (LSP – Liskov Substitution Principle). ToArray()



, , , , . , ToArray() , .

, ToArray()



– , , IEnumerable. , « » , , , , ToArray()



( ToList()



) . , 4- IoC : . : , :



public class Publisher { public Publisher(Subscriber[] subscribers) { this.subscribers = subscribers; } // … }





, IEnumerable, .

, , . , , Publisher . , .

, Publisher , , , , , - .

ICollection




, , , 7



IEnumerable - , .







LINQ LSP

IQueryable LSP, LINQ to Objects, , LINQ LSP.

LSP: . «» , , , . , IEnumerable:

new[] { "foo", "bar", "baz" };






:

public class InfiniteStrings : IEnumerable<string> { public IEnumerator<string> GetEnumerator() { while (true) yield return "foo"; } System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { return this.GetEnumerator(); } }







, ToArray()



( ToList()



) , ( ) . , , LSP- LINQ- :



Any()



AsEnumerable()



Concat(IEnumerable)





DefaultIfEmpty()





DefaultIfEmpty(T)





Distinct



(...)

Distinct(IEqualityComparer) (...)





ElementAt(int)





ElementAtOrDefault(int



)

First()





FirstOrDefault()





OfType()





Select(Func<TSource, TResult>)





Select(Func<TSource, int, TResult>)





SelectMany(Func<TSource, IEnumerable>)





SelectMany(Func<TSource, int, IEnumerable>)





SelectMany(Func<TSource, IEnumerable>, Func<TSource, TCollection, TResult>)





SelectMany(Func<TSource, int, IEnumerable>, Func<TSource, TCollection, TResult>)





Single()





SingleOrDefault()





Skip(int)





Take(int)





Where(Func<TSource, bool>)





Where(Func<TSource, int, bool>)





Zip(IEnumerable, Func<TFirst, TSecond, TResult>)









LINQ-, . , IEnumerable , .., LSP .

, , , , IReadOnlyCollection, : , , .NET 4.5. , .



.

, , IEnumerable . LSP- LINQ-, ( - ). .NET 4.5, IReadOnlyCollection



, IEnumerable. , .NET 4.5, ( -, , ).




















IEnumerable



public class Publisher { public Publisher(IEnumerable<Subscriber> subscribers) { // defensive copy -> good or bad? this.subscribers = subscribers.ToArray(); } // … }








« : IEnumerable, , . , ICollection



, ( ..: , . , , . ICollection



, , ? - .
)
. , , 'publisher' (, ). , , , . , , .

« , , -, - , IEnumerable



. , , – .

« , , – ( ), .







.



.

IEnumerable .NET. , , , (LSP – Liskov Substitution Principle). ToArray()



, , , , . , ToArray() , .

, ToArray()



– , , IEnumerable. , « » , , , , ToArray()



( ToList()



) . , 4- IoC : . : , :



public class Publisher { public Publisher(Subscriber[] subscribers) { this.subscribers = subscribers; } // … }





, IEnumerable, .

, , . , , Publisher . , .

, Publisher , , , , , - .

ICollection




, , , 7



IEnumerable - , .







LINQ LSP

IQueryable LSP, LINQ to Objects, , LINQ LSP.

LSP: . «» , , , . , IEnumerable:

new[] { "foo", "bar", "baz" };






:

public class InfiniteStrings : IEnumerable<string> { public IEnumerator<string> GetEnumerator() { while (true) yield return "foo"; } System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { return this.GetEnumerator(); } }







, ToArray()



( ToList()



) , ( ) . , , LSP- LINQ- :



Any()



AsEnumerable()



Concat(IEnumerable)





DefaultIfEmpty()





DefaultIfEmpty(T)





Distinct



(...)

Distinct(IEqualityComparer) (...)





ElementAt(int)





ElementAtOrDefault(int



)

First()





FirstOrDefault()





OfType()





Select(Func<TSource, TResult>)





Select(Func<TSource, int, TResult>)





SelectMany(Func<TSource, IEnumerable>)





SelectMany(Func<TSource, int, IEnumerable>)





SelectMany(Func<TSource, IEnumerable>, Func<TSource, TCollection, TResult>)





SelectMany(Func<TSource, int, IEnumerable>, Func<TSource, TCollection, TResult>)





Single()





SingleOrDefault()





Skip(int)





Take(int)





Where(Func<TSource, bool>)





Where(Func<TSource, int, bool>)





Zip(IEnumerable, Func<TFirst, TSecond, TResult>)









LINQ-, . , IEnumerable , .., LSP .

, , , , IReadOnlyCollection, : , , .NET 4.5. , .



.

, , IEnumerable . LSP- LINQ-, ( - ). .NET 4.5, IReadOnlyCollection



, IEnumerable. , .NET 4.5, ( -, , ).




















IEnumerable



public class Publisher { public Publisher(IEnumerable<Subscriber> subscribers) { // defensive copy -> good or bad? this.subscribers = subscribers.ToArray(); } // … }








« : IEnumerable, , . , ICollection



, ( ..: , . , , . ICollection



, , ? - .
)
. , , 'publisher' (, ). , , , . , , .

« , , -, - , IEnumerable



. , , – .

« , , – ( ), .







.



.

IEnumerable .NET. , , , (LSP – Liskov Substitution Principle). ToArray()



, , , , . , ToArray() , .

, ToArray()



– , , IEnumerable. , « » , , , , ToArray()



( ToList()



) . , 4- IoC : . : , :



public class Publisher { public Publisher(Subscriber[] subscribers) { this.subscribers = subscribers; } // … }





, IEnumerable, .

, , . , , Publisher . , .

, Publisher , , , , , - .

ICollection




, , , 7



IEnumerable - , .







LINQ LSP

IQueryable LSP, LINQ to Objects, , LINQ LSP.

LSP: . «» , , , . , IEnumerable:

new[] { "foo", "bar", "baz" };






:

public class InfiniteStrings : IEnumerable<string> { public IEnumerator<string> GetEnumerator() { while (true) yield return "foo"; } System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { return this.GetEnumerator(); } }







, ToArray()



( ToList()



) , ( ) . , , LSP- LINQ- :



Any()



AsEnumerable()



Concat(IEnumerable)





DefaultIfEmpty()





DefaultIfEmpty(T)





Distinct



(...)

Distinct(IEqualityComparer) (...)





ElementAt(int)





ElementAtOrDefault(int



)

First()





FirstOrDefault()





OfType()





Select(Func<TSource, TResult>)





Select(Func<TSource, int, TResult>)





SelectMany(Func<TSource, IEnumerable>)





SelectMany(Func<TSource, int, IEnumerable>)





SelectMany(Func<TSource, IEnumerable>, Func<TSource, TCollection, TResult>)





SelectMany(Func<TSource, int, IEnumerable>, Func<TSource, TCollection, TResult>)





Single()





SingleOrDefault()





Skip(int)





Take(int)





Where(Func<TSource, bool>)





Where(Func<TSource, int, bool>)





Zip(IEnumerable, Func<TFirst, TSecond, TResult>)









LINQ-, . , IEnumerable , .., LSP .

, , , , IReadOnlyCollection, : , , .NET 4.5. , .



.

, , IEnumerable . LSP- LINQ-, ( - ). .NET 4.5, IReadOnlyCollection



, IEnumerable. , .NET 4.5, ( -, , ).




















IEnumerable



public class Publisher { public Publisher(IEnumerable<Subscriber> subscribers) { // defensive copy -> good or bad? this.subscribers = subscribers.ToArray(); } // … }








« : IEnumerable, , . , ICollection



, ( ..: , . , , . ICollection



, , ? - .
)
. , , 'publisher' (, ). , , , . , , .

« , , -, - , IEnumerable



. , , – .

« , , – ( ), .







.



.

IEnumerable .NET. , , , (LSP – Liskov Substitution Principle). ToArray()



, , , , . , ToArray() , .

, ToArray()



– , , IEnumerable. , « » , , , , ToArray()



( ToList()



) . , 4- IoC : . : , :



public class Publisher { public Publisher(Subscriber[] subscribers) { this.subscribers = subscribers; } // … }





, IEnumerable, .

, , . , , Publisher . , .

, Publisher , , , , , - .

ICollection




, , , 7



IEnumerable - , .







LINQ LSP

IQueryable LSP, LINQ to Objects, , LINQ LSP.

LSP: . «» , , , . , IEnumerable:

new[] { "foo", "bar", "baz" };






:

public class InfiniteStrings : IEnumerable<string> { public IEnumerator<string> GetEnumerator() { while (true) yield return "foo"; } System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { return this.GetEnumerator(); } }







, ToArray()



( ToList()



) , ( ) . , , LSP- LINQ- :



Any()



AsEnumerable()



Concat(IEnumerable)





DefaultIfEmpty()





DefaultIfEmpty(T)





Distinct



(...)

Distinct(IEqualityComparer) (...)





ElementAt(int)





ElementAtOrDefault(int



)

First()





FirstOrDefault()





OfType()





Select(Func<TSource, TResult>)





Select(Func<TSource, int, TResult>)





SelectMany(Func<TSource, IEnumerable>)





SelectMany(Func<TSource, int, IEnumerable>)





SelectMany(Func<TSource, IEnumerable>, Func<TSource, TCollection, TResult>)





SelectMany(Func<TSource, int, IEnumerable>, Func<TSource, TCollection, TResult>)





Single()





SingleOrDefault()





Skip(int)





Take(int)





Where(Func<TSource, bool>)





Where(Func<TSource, int, bool>)





Zip(IEnumerable, Func<TFirst, TSecond, TResult>)









LINQ-, . , IEnumerable , .., LSP .

, , , , IReadOnlyCollection, : , , .NET 4.5. , .



.

, , IEnumerable . LSP- LINQ-, ( - ). .NET 4.5, IReadOnlyCollection



, IEnumerable. , .NET 4.5, ( -, , ).




















 IEnumerable 
      



public class Publisher { public Publisher(IEnumerable<Subscriber> subscribers) { // defensive copy -> good or bad? this.subscribers = subscribers.ToArray(); } // … }








« : IEnumerable, , . , ICollection



, ( ..: , . , , . ICollection



, , ? - .
)
. , , 'publisher' (, ). , , , . , , .

« , , -, - , IEnumerable



. , , – .

« , , – ( ), .







.



.

IEnumerable .NET. , , , (LSP – Liskov Substitution Principle). ToArray()



, , , , . , ToArray() , .

, ToArray()



– , , IEnumerable. , « » , , , , ToArray()



( ToList()



) . , 4- IoC : . : , :



public class Publisher { public Publisher(Subscriber[] subscribers) { this.subscribers = subscribers; } // … }





, IEnumerable, .

, , . , , Publisher . , .

, Publisher , , , , , - .

ICollection




, , , 7



IEnumerable - , .







LINQ LSP

IQueryable LSP, LINQ to Objects, , LINQ LSP.

LSP: . «» , , , . , IEnumerable:

new[] { "foo", "bar", "baz" };






:

public class InfiniteStrings : IEnumerable<string> { public IEnumerator<string> GetEnumerator() { while (true) yield return "foo"; } System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { return this.GetEnumerator(); } }







, ToArray()



( ToList()



) , ( ) . , , LSP- LINQ- :



Any()



AsEnumerable()



Concat(IEnumerable)





DefaultIfEmpty()





DefaultIfEmpty(T)





Distinct



(...)

Distinct(IEqualityComparer) (...)





ElementAt(int)





ElementAtOrDefault(int



)

First()





FirstOrDefault()





OfType()





Select(Func<TSource, TResult>)





Select(Func<TSource, int, TResult>)





SelectMany(Func<TSource, IEnumerable>)





SelectMany(Func<TSource, int, IEnumerable>)





SelectMany(Func<TSource, IEnumerable>, Func<TSource, TCollection, TResult>)





SelectMany(Func<TSource, int, IEnumerable>, Func<TSource, TCollection, TResult>)





Single()





SingleOrDefault()





Skip(int)





Take(int)





Where(Func<TSource, bool>)





Where(Func<TSource, int, bool>)





Zip(IEnumerable, Func<TFirst, TSecond, TResult>)









LINQ-, . , IEnumerable , .., LSP .

, , , , IReadOnlyCollection, : , , .NET 4.5. , .



.

, , IEnumerable . LSP- LINQ-, ( - ). .NET 4.5, IReadOnlyCollection



, IEnumerable. , .NET 4.5, ( -, , ).




















IEnumerable



public class Publisher { public Publisher(IEnumerable<Subscriber> subscribers) { // defensive copy -> good or bad? this.subscribers = subscribers.ToArray(); } // … }








« : IEnumerable, , . , ICollection



, ( ..: , . , , . ICollection



, , ? - .
)
. , , 'publisher' (, ). , , , . , , .

« , , -, - , IEnumerable



. , , – .

« , , – ( ), .







.



.

IEnumerable .NET. , , , (LSP – Liskov Substitution Principle). ToArray()



, , , , . , ToArray() , .

, ToArray()



– , , IEnumerable. , « » , , , , ToArray()



( ToList()



) . , 4- IoC : . : , :



public class Publisher { public Publisher(Subscriber[] subscribers) { this.subscribers = subscribers; } // … }





, IEnumerable, .

, , . , , Publisher . , .

, Publisher , , , , , - .

ICollection




, , , 7



IEnumerable - , .







LINQ LSP

IQueryable LSP, LINQ to Objects, , LINQ LSP.

LSP: . «» , , , . , IEnumerable:

new[] { "foo", "bar", "baz" };






:

public class InfiniteStrings : IEnumerable<string> { public IEnumerator<string> GetEnumerator() { while (true) yield return "foo"; } System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { return this.GetEnumerator(); } }







, ToArray()



( ToList()



) , ( ) . , , LSP- LINQ- :



Any()



AsEnumerable()



Concat(IEnumerable)





DefaultIfEmpty()





DefaultIfEmpty(T)





Distinct



(...)

Distinct(IEqualityComparer) (...)





ElementAt(int)





ElementAtOrDefault(int



)

First()





FirstOrDefault()





OfType()





Select(Func<TSource, TResult>)





Select(Func<TSource, int, TResult>)





SelectMany(Func<TSource, IEnumerable>)





SelectMany(Func<TSource, int, IEnumerable>)





SelectMany(Func<TSource, IEnumerable>, Func<TSource, TCollection, TResult>)





SelectMany(Func<TSource, int, IEnumerable>, Func<TSource, TCollection, TResult>)





Single()





SingleOrDefault()





Skip(int)





Take(int)





Where(Func<TSource, bool>)





Where(Func<TSource, int, bool>)





Zip(IEnumerable, Func<TFirst, TSecond, TResult>)









LINQ-, . , IEnumerable , .., LSP .

, , , , IReadOnlyCollection, : , , .NET 4.5. , .



.

, , IEnumerable . LSP- LINQ-, ( - ). .NET 4.5, IReadOnlyCollection



, IEnumerable. , .NET 4.5, ( -, , ).




















 IEnumerable 
      



public class Publisher { public Publisher(IEnumerable<Subscriber> subscribers) { // defensive copy -> good or bad? this.subscribers = subscribers.ToArray(); } // … }




















« : IEnumerable, , . , ICollection



, ( ..: , . , , . ICollection



, , ? - .
)
. , , 'publisher' (, ). , , , . , , .

« , , -, - , IEnumerable



. , , – .

« , , – ( ), .







.



.

IEnumerable .NET. , , , (LSP – Liskov Substitution Principle). ToArray()



, , , , . , ToArray() , .

, ToArray()



– , , IEnumerable. , « » , , , , ToArray()



( ToList()



) . , 4- IoC : . : , :



public class Publisher { public Publisher(Subscriber[] subscribers) { this.subscribers = subscribers; } // … }





, IEnumerable, .

, , . , , Publisher . , .

, Publisher , , , , , - .

ICollection




, , , 7 IEnumerable - , .



LINQ LSP

IQueryable LSP, LINQ to Objects, , LINQ LSP.

LSP: . «» , , , . , IEnumerable:

new[] { "foo", "bar", "baz" };

:

public class InfiniteStrings : IEnumerable<string> { public IEnumerator<string> GetEnumerator() { while (true) yield return "foo"; } System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { return this.GetEnumerator(); } }







, ToArray()



( ToList()



) , ( ) . , , LSP- LINQ- :



Any()



AsEnumerable()



Concat(IEnumerable)





DefaultIfEmpty()





DefaultIfEmpty(T)





Distinct



(...)

Distinct(IEqualityComparer) (...)





ElementAt(int)





ElementAtOrDefault(int



)

First()





FirstOrDefault()





OfType()





Select(Func<TSource, TResult>)





Select(Func<TSource, int, TResult>)





SelectMany(Func<TSource, IEnumerable>)





SelectMany(Func<TSource, int, IEnumerable>)





SelectMany(Func<TSource, IEnumerable>, Func<TSource, TCollection, TResult>)





SelectMany(Func<TSource, int, IEnumerable>, Func<TSource, TCollection, TResult>)





Single()





SingleOrDefault()





Skip(int)





Take(int)





Where(Func<TSource, bool>)





Where(Func<TSource, int, bool>)





Zip(IEnumerable, Func<TFirst, TSecond, TResult>)









LINQ-, . , IEnumerable , .., LSP .

, , , , IReadOnlyCollection, : , , .NET 4.5. , .



.

, , IEnumerable . LSP- LINQ-, ( - ). .NET 4.5, IReadOnlyCollection



, IEnumerable. , .NET 4.5, ( -, , ).




























IEnumerable



public class Publisher { public Publisher(IEnumerable<Subscriber> subscribers) { // defensive copy -> good or bad? this.subscribers = subscribers.ToArray(); } // … }
















« : IEnumerable, , . , ICollection



, ( ..: , . , , . ICollection



, , ? - .
)
. , , 'publisher' (, ). , , , . , , .

« , , -, - , IEnumerable



. , , – .

« , , – ( ), .







.



.

IEnumerable .NET. , , , (LSP – Liskov Substitution Principle). ToArray()



, , , , . , ToArray() , .

, ToArray()



– , , IEnumerable. , « » , , , , ToArray()



( ToList()



) . , 4- IoC : . : , :



public class Publisher { public Publisher(Subscriber[] subscribers) { this.subscribers = subscribers; } // … }





, IEnumerable, .

, , . , , Publisher . , .

, Publisher , , , , , - .

ICollection




, , , 7 IEnumerable - , .







LINQ LSP

IQueryable LSP, LINQ to Objects, , LINQ LSP.

LSP: . «» , , , . , IEnumerable:

new[] { "foo", "bar", "baz" };

:

public class InfiniteStrings : IEnumerable<string> { public IEnumerator<string> GetEnumerator() { while (true) yield return "foo"; } System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { return this.GetEnumerator(); } }







, ToArray()



( ToList()



) , ( ) . , , LSP- LINQ- :



Any()



AsEnumerable()



Concat(IEnumerable)





DefaultIfEmpty()





DefaultIfEmpty(T)





Distinct



(...)

Distinct(IEqualityComparer) (...)





ElementAt(int)





ElementAtOrDefault(int



)

First()





FirstOrDefault()





OfType()





Select(Func<TSource, TResult>)





Select(Func<TSource, int, TResult>)





SelectMany(Func<TSource, IEnumerable>)





SelectMany(Func<TSource, int, IEnumerable>)





SelectMany(Func<TSource, IEnumerable>, Func<TSource, TCollection, TResult>)





SelectMany(Func<TSource, int, IEnumerable>, Func<TSource, TCollection, TResult>)





Single()





SingleOrDefault()





Skip(int)





Take(int)





Where(Func<TSource, bool>)





Where(Func<TSource, int, bool>)





Zip(IEnumerable, Func<TFirst, TSecond, TResult>)









LINQ-, . , IEnumerable , .., LSP .

, , , , IReadOnlyCollection, : , , .NET 4.5. , .



.

, , IEnumerable . LSP- LINQ-, ( - ). .NET 4.5, IReadOnlyCollection



, IEnumerable. , .NET 4.5, ( -, , ).
























IEnumerable



public class Publisher { public Publisher(IEnumerable<Subscriber> subscribers) { // defensive copy -> good or bad? this.subscribers = subscribers.ToArray(); } // … }
















« : IEnumerable, , . , ICollection



, ( ..: , . , , . ICollection



, , ? - .
)
. , , 'publisher' (, ). , , , . , , .

« , , -, - , IEnumerable



. , , – .

« , , – ( ), .







.



.

IEnumerable .NET. , , , (LSP – Liskov Substitution Principle). ToArray()



, , , , . , ToArray() , .

, ToArray()



– , , IEnumerable. , « » , , , , ToArray()



( ToList()



) . , 4- IoC : . : , :



public class Publisher { public Publisher(Subscriber[] subscribers) { this.subscribers = subscribers; } // … }





, IEnumerable, .

, , . , , Publisher . , .

, Publisher , , , , , - .

ICollection




, , , 7 IEnumerable - , .







LINQ LSP

IQueryable LSP, LINQ to Objects, , LINQ LSP.

LSP: . «» , , , . , IEnumerable:

new[] { "foo", "bar", "baz" };

:

public class InfiniteStrings : IEnumerable<string> { public IEnumerator<string> GetEnumerator() { while (true) yield return "foo"; } System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { return this.GetEnumerator(); } }







, ToArray()



( ToList()



) , ( ) . , , LSP- LINQ- :



Any()



AsEnumerable()



Concat(IEnumerable)





DefaultIfEmpty()





DefaultIfEmpty(T)





Distinct



(...)

Distinct(IEqualityComparer) (...)





ElementAt(int)





ElementAtOrDefault(int



)

First()





FirstOrDefault()





OfType()





Select(Func<TSource, TResult>)





Select(Func<TSource, int, TResult>)





SelectMany(Func<TSource, IEnumerable>)





SelectMany(Func<TSource, int, IEnumerable>)





SelectMany(Func<TSource, IEnumerable>, Func<TSource, TCollection, TResult>)





SelectMany(Func<TSource, int, IEnumerable>, Func<TSource, TCollection, TResult>)





Single()





SingleOrDefault()





Skip(int)





Take(int)





Where(Func<TSource, bool>)





Where(Func<TSource, int, bool>)





Zip(IEnumerable, Func<TFirst, TSecond, TResult>)









LINQ-, . , IEnumerable , .., LSP .

, , , , IReadOnlyCollection, : , , .NET 4.5. , .



.

, , IEnumerable . LSP- LINQ-, ( - ). .NET 4.5, IReadOnlyCollection



, IEnumerable. , .NET 4.5, ( -, , ).
























IEnumerable



public class Publisher { public Publisher(IEnumerable<Subscriber> subscribers) { // defensive copy -> good or bad? this.subscribers = subscribers.ToArray(); } // … }












« : IEnumerable, , . , ICollection



, ( ..: , . , , . ICollection



, , ? - .
)
. , , 'publisher' (, ). , , , . , , .

« , , -, - , IEnumerable



. , , – .

« , , – ( ), .







.



.

IEnumerable .NET. , , , (LSP – Liskov Substitution Principle). ToArray()



, , , , . , ToArray() , .

, ToArray()



– , , IEnumerable. , « » , , , , ToArray()



( ToList()



) . , 4- IoC : . : , :



public class Publisher { public Publisher(Subscriber[] subscribers) { this.subscribers = subscribers; } // … }





, IEnumerable, .

, , . , , Publisher . , .

, Publisher , , , , , - .

ICollection




, , , 7 IEnumerable - , .







LINQ LSP

IQueryable LSP, LINQ to Objects, , LINQ LSP.

LSP: . «» , , , . , IEnumerable:

new[] { "foo", "bar", "baz" };






:

public class InfiniteStrings : IEnumerable<string> { public IEnumerator<string> GetEnumerator() { while (true) yield return "foo"; } System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { return this.GetEnumerator(); } }







, ToArray()



( ToList()



) , ( ) . , , LSP- LINQ- :



Any()



AsEnumerable()



Concat(IEnumerable)





DefaultIfEmpty()





DefaultIfEmpty(T)





Distinct



(...)

Distinct(IEqualityComparer) (...)





ElementAt(int)





ElementAtOrDefault(int



)

First()





FirstOrDefault()





OfType()





Select(Func<TSource, TResult>)





Select(Func<TSource, int, TResult>)





SelectMany(Func<TSource, IEnumerable>)





SelectMany(Func<TSource, int, IEnumerable>)





SelectMany(Func<TSource, IEnumerable>, Func<TSource, TCollection, TResult>)





SelectMany(Func<TSource, int, IEnumerable>, Func<TSource, TCollection, TResult>)





Single()





SingleOrDefault()





Skip(int)





Take(int)





Where(Func<TSource, bool>)





Where(Func<TSource, int, bool>)





Zip(IEnumerable, Func<TFirst, TSecond, TResult>)









LINQ-, . , IEnumerable , .., LSP .

, , , , IReadOnlyCollection, : , , .NET 4.5. , .



.

, , IEnumerable . LSP- LINQ-, ( - ). .NET 4.5, IReadOnlyCollection



, IEnumerable. , .NET 4.5, ( -, , ).




















 IEnumerable 
      



public class Publisher { public Publisher(IEnumerable<Subscriber> subscribers) { // defensive copy -> good or bad? this.subscribers = subscribers.ToArray(); } // … }








« : IEnumerable, , . , ICollection



, ( ..: , . , , . ICollection



, , ? - .
)
. , , 'publisher' (, ). , , , . , , .

« , , -, - , IEnumerable



. , , – .

« , , – ( ), .







.



.

IEnumerable .NET. , , , (LSP – Liskov Substitution Principle). ToArray()



, , , , . , ToArray() , .

, ToArray()



– , , IEnumerable. , « » , , , , ToArray()



( ToList()



) . , 4- IoC : . : , :



public class Publisher { public Publisher(Subscriber[] subscribers) { this.subscribers = subscribers; } // … }





, IEnumerable, .

, , . , , Publisher . , .

, Publisher , , , , , - .

ICollection




, , , 7



IEnumerable - , .







LINQ LSP

IQueryable LSP, LINQ to Objects, , LINQ LSP.

LSP: . «» , , , . , IEnumerable:

new[] { "foo", "bar", "baz" };






:

public class InfiniteStrings : IEnumerable<string> { public IEnumerator<string> GetEnumerator() { while (true) yield return "foo"; } System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { return this.GetEnumerator(); } }







, ToArray()



( ToList()



) , ( ) . , , LSP- LINQ- :



Any()



AsEnumerable()



Concat(IEnumerable)





DefaultIfEmpty()





DefaultIfEmpty(T)





Distinct



(...)

Distinct(IEqualityComparer) (...)





ElementAt(int)





ElementAtOrDefault(int



)

First()





FirstOrDefault()





OfType()





Select(Func<TSource, TResult>)





Select(Func<TSource, int, TResult>)





SelectMany(Func<TSource, IEnumerable>)





SelectMany(Func<TSource, int, IEnumerable>)





SelectMany(Func<TSource, IEnumerable>, Func<TSource, TCollection, TResult>)





SelectMany(Func<TSource, int, IEnumerable>, Func<TSource, TCollection, TResult>)





Single()





SingleOrDefault()





Skip(int)





Take(int)





Where(Func<TSource, bool>)





Where(Func<TSource, int, bool>)





Zip(IEnumerable, Func<TFirst, TSecond, TResult>)









LINQ-, . , IEnumerable , .., LSP .

, , , , IReadOnlyCollection, : , , .NET 4.5. , .



.

, , IEnumerable . LSP- LINQ-, ( - ). .NET 4.5, IReadOnlyCollection



, IEnumerable. , .NET 4.5, ( -, , ).




















IEnumerable



public class Publisher { public Publisher(IEnumerable<Subscriber> subscribers) { // defensive copy -> good or bad? this.subscribers = subscribers.ToArray(); } // … }












« : IEnumerable, , . , ICollection



, ( ..: , . , , . ICollection



, , ? - .
)
. , , 'publisher' (, ). , , , . , , .

« , , -, - , IEnumerable



. , , – .

« , , – ( ), .







.



.

IEnumerable .NET. , , , (LSP – Liskov Substitution Principle). ToArray()



, , , , . , ToArray() , .

, ToArray()



– , , IEnumerable. , « » , , , , ToArray()



( ToList()



) . , 4- IoC : . : , :



public class Publisher { public Publisher(Subscriber[] subscribers) { this.subscribers = subscribers; } // … }





, IEnumerable, .

, , . , , Publisher . , .

, Publisher , , , , , - .

ICollection




, , , 7 IEnumerable - , .







LINQ LSP

IQueryable LSP, LINQ to Objects, , LINQ LSP.

LSP: . «» , , , . , IEnumerable:

new[] { "foo", "bar", "baz" };






:

public class InfiniteStrings : IEnumerable<string> { public IEnumerator<string> GetEnumerator() { while (true) yield return "foo"; } System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { return this.GetEnumerator(); } }







, ToArray()



( ToList()



) , ( ) . , , LSP- LINQ- :



Any()



AsEnumerable()



Concat(IEnumerable)





DefaultIfEmpty()





DefaultIfEmpty(T)





Distinct



(...)

Distinct(IEqualityComparer) (...)





ElementAt(int)





ElementAtOrDefault(int



)

First()





FirstOrDefault()





OfType()





Select(Func<TSource, TResult>)





Select(Func<TSource, int, TResult>)





SelectMany(Func<TSource, IEnumerable>)





SelectMany(Func<TSource, int, IEnumerable>)





SelectMany(Func<TSource, IEnumerable>, Func<TSource, TCollection, TResult>)





SelectMany(Func<TSource, int, IEnumerable>, Func<TSource, TCollection, TResult>)





Single()





SingleOrDefault()





Skip(int)





Take(int)





Where(Func<TSource, bool>)





Where(Func<TSource, int, bool>)





Zip(IEnumerable, Func<TFirst, TSecond, TResult>)









LINQ-, . , IEnumerable , .., LSP .

, , , , IReadOnlyCollection, : , , .NET 4.5. , .



.

, , IEnumerable . LSP- LINQ-, ( - ). .NET 4.5, IReadOnlyCollection



, IEnumerable. , .NET 4.5, ( -, , ).




















  • IEnumerable



    public class Publisher { public Publisher(IEnumerable<Subscriber> subscribers) { // defensive copy -> good or bad? this.subscribers = subscribers.ToArray(); } // … }
















    « : IEnumerable, , . , ICollection



    , ( ..: , . , , . ICollection



    , , ? - .
    )
    . , , 'publisher' (, ). , , , . , , .

    « , , -, - , IEnumerable



    . , , – .

    « , , – ( ), .







    .



    .

    IEnumerable .NET. , , , (LSP – Liskov Substitution Principle). ToArray()



    , , , , . , ToArray() , .

    , ToArray()



    – , , IEnumerable. , « » , , , , ToArray()



    ( ToList()



    ) . , 4- IoC : . : , :



    public class Publisher { public Publisher(Subscriber[] subscribers) { this.subscribers = subscribers; } // … }





    , IEnumerable, .

    , , . , , Publisher . , .

    , Publisher , , , , , - .

    ICollection




    , , , 7 IEnumerable - , .







    LINQ LSP

    IQueryable LSP, LINQ to Objects, , LINQ LSP.

    LSP: . «» , , , . , IEnumerable:

    new[] { "foo", "bar", "baz" };






    :

    public class InfiniteStrings : IEnumerable<string> { public IEnumerator<string> GetEnumerator() { while (true) yield return "foo"; } System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { return this.GetEnumerator(); } }







    , ToArray()



    ( ToList()



    ) , ( ) . , , LSP- LINQ- :



    Any() AsEnumerable()



    Concat(IEnumerable)





    DefaultIfEmpty()





    DefaultIfEmpty(T)





    Distinct



    (...)

    Distinct(IEqualityComparer) (...)





    ElementAt(int)





    ElementAtOrDefault(int



    )

    First()





    FirstOrDefault()





    OfType()





    Select(Func<TSource, TResult>)





    Select(Func<TSource, int, TResult>)





    SelectMany(Func<TSource, IEnumerable>)





    SelectMany(Func<TSource, int, IEnumerable>)





    SelectMany(Func<TSource, IEnumerable>, Func<TSource, TCollection, TResult>)





    SelectMany(Func<TSource, int, IEnumerable>, Func<TSource, TCollection, TResult>)





    Single()





    SingleOrDefault()





    Skip(int)





    Take(int)





    Where(Func<TSource, bool>)





    Where(Func<TSource, int, bool>)





    Zip(IEnumerable, Func<TFirst, TSecond, TResult>)









    LINQ-, . , IEnumerable , .., LSP .

    , , , , IReadOnlyCollection, : , , .NET 4.5. , .



    .

    , , IEnumerable . LSP- LINQ-, ( - ). .NET 4.5, IReadOnlyCollection



    , IEnumerable. , .NET 4.5, ( -, , ).
























  • IEnumerable



    public class Publisher { public Publisher(IEnumerable<Subscriber> subscribers) { // defensive copy -> good or bad? this.subscribers = subscribers.ToArray(); } // … }
















    « : IEnumerable, , . , ICollection



    , ( ..: , . , , . ICollection



    , , ? - .
    )
    . , , 'publisher' (, ). , , , . , , .

    « , , -, - , IEnumerable



    . , , – .

    « , , – ( ), .







    .



    .

    IEnumerable .NET. , , , (LSP – Liskov Substitution Principle). ToArray()



    , , , , . , ToArray() , .

    , ToArray()



    – , , IEnumerable. , « » , , , , ToArray()



    ( ToList()



    ) . , 4- IoC : . : , :



    public class Publisher { public Publisher(Subscriber[] subscribers) { this.subscribers = subscribers; } // … }





    , IEnumerable, .

    , , . , , Publisher . , .

    , Publisher , , , , , - .

    ICollection




    , , , 7 IEnumerable - , .







    LINQ LSP

    IQueryable LSP, LINQ to Objects, , LINQ LSP.

    LSP: . «» , , , . , IEnumerable:

    new[] { "foo", "bar", "baz" };






    :

    public class InfiniteStrings : IEnumerable<string> { public IEnumerator<string> GetEnumerator() { while (true) yield return "foo"; } System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { return this.GetEnumerator(); } }







    , ToArray()



    ( ToList()



    ) , ( ) . , , LSP- LINQ- :



    Any()



    AsEnumerable() Concat(IEnumerable)





    DefaultIfEmpty()





    DefaultIfEmpty(T)





    Distinct



    (...)

    Distinct(IEqualityComparer) (...)





    ElementAt(int)





    ElementAtOrDefault(int



    )

    First()





    FirstOrDefault()





    OfType()





    Select(Func<TSource, TResult>)





    Select(Func<TSource, int, TResult>)





    SelectMany(Func<TSource, IEnumerable>)





    SelectMany(Func<TSource, int, IEnumerable>)





    SelectMany(Func<TSource, IEnumerable>, Func<TSource, TCollection, TResult>)





    SelectMany(Func<TSource, int, IEnumerable>, Func<TSource, TCollection, TResult>)





    Single()





    SingleOrDefault()





    Skip(int)





    Take(int)





    Where(Func<TSource, bool>)





    Where(Func<TSource, int, bool>)





    Zip(IEnumerable, Func<TFirst, TSecond, TResult>)









    LINQ-, . , IEnumerable , .., LSP .

    , , , , IReadOnlyCollection, : , , .NET 4.5. , .



    .

    , , IEnumerable . LSP- LINQ-, ( - ). .NET 4.5, IReadOnlyCollection



    , IEnumerable. , .NET 4.5, ( -, , ).
























  • IEnumerable



    public class Publisher { public Publisher(IEnumerable<Subscriber> subscribers) { // defensive copy -> good or bad? this.subscribers = subscribers.ToArray(); } // … }












    « : IEnumerable, , . , ICollection



    , ( ..: , . , , . ICollection



    , , ? - .
    )
    . , , 'publisher' (, ). , , , . , , .

    « , , -, - , IEnumerable



    . , , – .

    « , , – ( ), .







    .



    .

    IEnumerable .NET. , , , (LSP – Liskov Substitution Principle). ToArray()



    , , , , . , ToArray() , .

    , ToArray()



    – , , IEnumerable. , « » , , , , ToArray()



    ( ToList()



    ) . , 4- IoC : . : , :



    public class Publisher { public Publisher(Subscriber[] subscribers) { this.subscribers = subscribers; } // … }





    , IEnumerable, .

    , , . , , Publisher . , .

    , Publisher , , , , , - .

    ICollection




    , , , 7 IEnumerable - , .







    LINQ LSP

    IQueryable LSP, LINQ to Objects, , LINQ LSP.

    LSP: . «» , , , . , IEnumerable:

    new[] { "foo", "bar", "baz" };






    :

    public class InfiniteStrings : IEnumerable<string> { public IEnumerator<string> GetEnumerator() { while (true) yield return "foo"; } System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { return this.GetEnumerator(); } }







    , ToArray()



    ( ToList()



    ) , ( ) . , , LSP- LINQ- :



    Any()



    AsEnumerable()



    Concat(IEnumerable)





    DefaultIfEmpty()





    DefaultIfEmpty(T)





    Distinct



    (...)

    Distinct(IEqualityComparer) (...)





    ElementAt(int)





    ElementAtOrDefault(int



    )

    First()





    FirstOrDefault()





    OfType()





    Select(Func<TSource, TResult>)





    Select(Func<TSource, int, TResult>)





    SelectMany(Func<TSource, IEnumerable>)





    SelectMany(Func<TSource, int, IEnumerable>)





    SelectMany(Func<TSource, IEnumerable>, Func<TSource, TCollection, TResult>)





    SelectMany(Func<TSource, int, IEnumerable>, Func<TSource, TCollection, TResult>)





    Single()





    SingleOrDefault()





    Skip(int)





    Take(int)





    Where(Func<TSource, bool>)





    Where(Func<TSource, int, bool>)





    Zip(IEnumerable, Func<TFirst, TSecond, TResult>)









    LINQ-, . , IEnumerable , .., LSP .

    , , , , IReadOnlyCollection, : , , .NET 4.5. , .



    .

    , , IEnumerable . LSP- LINQ-, ( - ). .NET 4.5, IReadOnlyCollection



    , IEnumerable. , .NET 4.5, ( -, , ).




















    IEnumerable



    public class Publisher { public Publisher(IEnumerable<Subscriber> subscribers) { // defensive copy -> good or bad? this.subscribers = subscribers.ToArray(); } // … }












    « : IEnumerable, , . , ICollection



    , ( ..: , . , , . ICollection



    , , ? - .
    )
    . , , 'publisher' (, ). , , , . , , .

    « , , -, - , IEnumerable



    . , , – .

    « , , – ( ), .







    .



    .

    IEnumerable .NET. , , , (LSP – Liskov Substitution Principle). ToArray()



    , , , , . , ToArray() , .

    , ToArray()



    – , , IEnumerable. , « » , , , , ToArray()



    ( ToList()



    ) . , 4- IoC : . : , :



    public class Publisher { public Publisher(Subscriber[] subscribers) { this.subscribers = subscribers; } // … }





    , IEnumerable, .

    , , . , , Publisher . , .

    , Publisher , , , , , - .

    ICollection




    , , , 7 IEnumerable - , .







    LINQ LSP

    IQueryable LSP, LINQ to Objects, , LINQ LSP.

    LSP: . «» , , , . , IEnumerable:

    new[] { "foo", "bar", "baz" };






    :

    public class InfiniteStrings : IEnumerable<string> { public IEnumerator<string> GetEnumerator() { while (true) yield return "foo"; } System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { return this.GetEnumerator(); } }







    , ToArray()



    ( ToList()



    ) , ( ) . , , LSP- LINQ- :



    Any()



    AsEnumerable()



    Concat(IEnumerable)





    DefaultIfEmpty()





    DefaultIfEmpty(T)





    Distinct



    (...)

    Distinct(IEqualityComparer) (...)





    ElementAt(int)





    ElementAtOrDefault(int



    )

    First()





    FirstOrDefault()





    OfType()





    Select(Func<TSource, TResult>)





    Select(Func<TSource, int, TResult>)





    SelectMany(Func<TSource, IEnumerable>)





    SelectMany(Func<TSource, int, IEnumerable>)





    SelectMany(Func<TSource, IEnumerable>, Func<TSource, TCollection, TResult>)





    SelectMany(Func<TSource, int, IEnumerable>, Func<TSource, TCollection, TResult>)





    Single()





    SingleOrDefault()





    Skip(int)





    Take(int)





    Where(Func<TSource, bool>)





    Where(Func<TSource, int, bool>)





    Zip(IEnumerable, Func<TFirst, TSecond, TResult>)









    LINQ-, . , IEnumerable , .., LSP .

    , , , , IReadOnlyCollection, : , , .NET 4.5. , .



    .

    , , IEnumerable . LSP- LINQ-, ( - ). .NET 4.5, IReadOnlyCollection



    , IEnumerable. , .NET 4.5, ( -, , ).




















    IEnumerable



    public class Publisher { public Publisher(IEnumerable<Subscriber> subscribers) { // defensive copy -> good or bad? this.subscribers = subscribers.ToArray(); } // … }












    « : IEnumerable, , . , ICollection



    , ( ..: , . , , . ICollection



    , , ? - .
    )
    . , , 'publisher' (, ). , , , . , , .

    « , , -, - , IEnumerable



    . , , – .

    « , , – ( ), .







    .



    .

    IEnumerable .NET. , , , (LSP – Liskov Substitution Principle). ToArray()



    , , , , . , ToArray() , .

    , ToArray()



    – , , IEnumerable. , « » , , , , ToArray()



    ( ToList()



    ) . , 4- IoC : . : , :



    public class Publisher { public Publisher(Subscriber[] subscribers) { this.subscribers = subscribers; } // … }





    , IEnumerable, .

    , , . , , Publisher . , .

    , Publisher , , , , , - .

    ICollection




    , , , 7 IEnumerable - , .







    LINQ LSP

    IQueryable LSP, LINQ to Objects, , LINQ LSP.

    LSP: . «» , , , . , IEnumerable:

    new[] { "foo", "bar", "baz" };






    :

    public class InfiniteStrings : IEnumerable<string> { public IEnumerator<string> GetEnumerator() { while (true) yield return "foo"; } System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { return this.GetEnumerator(); } }







    , ToArray()



    ( ToList()



    ) , ( ) . , , LSP- LINQ- :



    Any()



    AsEnumerable()



    Concat(IEnumerable)





    DefaultIfEmpty()





    DefaultIfEmpty(T)





    Distinct



    (...)

    Distinct(IEqualityComparer) (...)





    ElementAt(int)





    ElementAtOrDefault(int



    )

    First()





    FirstOrDefault()





    OfType()





    Select(Func<TSource, TResult>)





    Select(Func<TSource, int, TResult>)





    SelectMany(Func<TSource, IEnumerable>)





    SelectMany(Func<TSource, int, IEnumerable>)





    SelectMany(Func<TSource, IEnumerable>, Func<TSource, TCollection, TResult>)





    SelectMany(Func<TSource, int, IEnumerable>, Func<TSource, TCollection, TResult>)





    Single()





    SingleOrDefault()





    Skip(int)





    Take(int)





    Where(Func<TSource, bool>)





    Where(Func<TSource, int, bool>)





    Zip(IEnumerable, Func<TFirst, TSecond, TResult>)









    LINQ-, . , IEnumerable , .., LSP .

    , , , , IReadOnlyCollection, : , , .NET 4.5. , .



    .

    , , IEnumerable . LSP- LINQ-, ( - ). .NET 4.5, IReadOnlyCollection



    , IEnumerable. , .NET 4.5, ( -, , ).























All Articles