C# Linq unique not work on lists -


this question has answer here:

i trying following code check if list contains duplicated data:

    internal class program {     private static void main(string[] args)     {         var list = new list<obj>() { new obj() { id = "1", name = "1" }, new obj() { id = "1", name = "1" } };         console.writeline(allitemsareunique(list));     }      public static bool allitemsareunique<t>(ienumerable<t> items)     {         return items.distinct().count() == items.count();     } }  internal class obj {     public string id;     public string name; } 

and result true! why?

why?

by default, comparison use references , in case, 2 object references not same.

you need implement iequatable<t> provide type-specific equals() method distinct() use.


Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

How to get the ip address of VM and use it to configure SSH connection dynamically in Ansible -

javascript - Get parameter of GET request -