To count the unique entries in a list in C#, you can use the LINQ extension method Distinct()
to get a new list containing only the unique values, and then use the Count()
method to get the number of elements in that new list.
Here's an example:
main.cs334 chars16 lines
This code defines a List<int>
called numbers
and initializes it with some integers. It then uses the Distinct()
and Count()
methods to count the number of unique entries in the list, which is 3. The result is printed to the console using Console.WriteLine()
.
gistlibby LogSnag