views:

1329

answers:

2
+2  Q: 

.NET List.Distinct

I'm using .NET 3.5. Why am I still be getting:

does not contain a definition for 'Distinct'

with this code:

using System.Collections.Generic;

       //.. . . . . code


    List<string> Words = new List<string>();
       // many strings added here . . .
    Words = Words.Distinct().ToList();
+18  A: 

Are you

using System.Linq;

?

Distinct is an extension method defined in System.Linq.Enumerable so you need to add that using statement.

And don't forget to add a reference to System.Core.dll (if you're using VS2008, this has already been done for you).

Martinho Fernandes
+1 for System.Core
Jeremy McGee
Is there a reason for naming it like that?
Martinho Fernandes
Martinho -- naming _what_ like _what_?
Eric Lippert
System.Core. It's mainly extensions... So why Core?
Martinho Fernandes
Ah. No particularly good reason that I'm aware of. What would you have named it?
Eric Lippert
I'm not very good at names, but I think Core is counter-intuitive, since there are a lot of extensions in there. It's not really that important, though. I was just curious about it.
Martinho Fernandes
I would imagine that "Core" here means "something that should really be in mscorlib, but we couldn't put it there for backcompat reasons" :)
Pavel Minaev
@Pavel: That sounds more like it.
Martinho Fernandes
+3  A: 

You forgot to add

using System.Linq;

Distinct is an extension method that is defined in System.Linq.Enumerable, so you can only call it if you import that namespace.

You'll also need to add a reference to System.Core.dll.
If you created the project as a .Net 3.5 project, it will already be referenced; if you upgraded it from .Net 2 or 3, you'll have to add the reference yourself.

SLaks