Union Vs Concat in Linq

前端 未结 3 1340
心在旅途
心在旅途 2020-12-02 21:55

I have a question on Union and Concat. I guess both are behaving same in case of List .

var a1 = (new[] { 1,          


        
3条回答
  •  有刺的猬
    2020-12-02 22:19

    Concat literally returns the items from the first sequence followed by the items from the second sequence. If you use Concat on two 2-item sequences, you will always get a 4-item sequence.

    Union is essentially Concat followed by Distinct.

    In your first two cases, you end up with 2-item sequences because, between them, each pair of input squences has exactly two distinct items.

    In your third case, you end up with a 4-item sequence because all four items in your two input sequences are distinct.

提交回复
热议问题