Why is Enumerable.Except used for a string array in C #?

Example

Here is an example of the code I found on the Pete on Software blog :

var listThree = new string[] { "Pete", "On", "Software" };  
var listFour = new string[] { "Joel", "On", "Software" };  
stringExcept = listThree.Except(listFour);

The code compiles and runs. So far so good.

Question

However, I do not understand why it works.

So, can anyone explain why I can use Enumerable.Exceptin an array of strings?

Perhaps it will be clear to me if anyone can explain how to read the signature Enumerable.Exceptand give me a code example:

public static IEnumerable<TSource> Except<TSource>(
    this IEnumerable<TSource> first,
    IEnumerable<TSource> second
)

What i know

I know the concepts of generics and extension methods. But, obviously, it’s not good enough to understand the code example above. I also already used some basic Linq queries.

+3
source share
5 answers

Except - , , IEnumerable<T>. System.Array , IEnumerable<T>.

, System.Array IEnumerable<T>

.NET Framework 2.0 Array System.Collections.Generic.IList<T>, System.Collections.Generic.ICollection<T> System.Collections.Generic.IEnumerable<T>. . Array, , ( ). , , - , , throw NotSupportedException.

+4

, IEnumerable TSource string, IEnumerable IEnumerable . , IEnumerable (, , ).

+2

T (, , a T[]), IEnumerable<T>. T System.String. Enumerable.Except - IEnumerable<T>, string[]. stringExcept = listThree.Except(listFour);

stringExcept = Enumerable.Except(listThree, listFour).
+2

TSource , IEnumerable<string> , , . , - :

  • string[] IEnumerable<string>
  • ,
+1

The Except method returns elements in the first enumeration, which also do not appear in the second enumeration. Therefore, in the case indicated by you, the result will be {"Pete", "Joel"}.

In this case, thinking in terms of string arrays is perhaps a red herring. It might be more beneficial to think in terms of equality of objects ( http://msdn.microsoft.com/en-us/library/system.object.equals.aspx) .

Microsoft documentation is here: http://msdn.microsoft.com/en-us/library/system.linq.enumerable.except.aspx

0
source

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


All Articles