LINQ बनाम एलएसपी

रक्षात्मक प्रोग्रामिंग पर मेरी पिछली पोस्ट के जवाब में, मेरे एक पाठक ने मुझे यह सवाल भेजा:
[एक] एक बहुत प्रसिद्ध रक्षात्मक प्रोग्रामिंग परिदृश्य तब होता है जब इनपुट पैरामीटर होता है
 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, ( -, , ).


Source: https://habr.com/ru/post/In191770/


All Articles