DISABLE ADBLOCK

ADBlock is blocking some content on the site

ADBlock errore
Results found: 

Question

I don't have a programming problem, but I'd like to know why.

I have something like this

List<MyClass> a,b,c<br /><br />var d = a.Except(b);<br />c.Clear()<br />c = d.ToList();


Of course, lists a & b are populated preceding the code. The question is, why can't I cast directly to List<myclass> as such:

<br />c = a.Except(b);<br />


Solution

You can't cast it as such because a.Except(b) returns the type System.Linq.Enumerable.ExceptIterator. You have two options here:

c.AddRange(a.Except(b));
or
c = a.Except(b).ToList();


Solution 2

because they're basically different types. a.Except(b) does not return a list, it returns an "Enumerable" type.

so

IEnumerable c
List a,b

c = a.Except(b)

will work

If you like, leave us your opinion

Was the article helpful and is it translated correctly?

Licensed under: CC-BY-SA with attribution
Not affiliated with codeproject.com