C# List Examples
The collection classes of List<T> class are defined in the System.Collection.Generic class and are designed for grouping objects and performing tasks on them. In this blog, I will show you how to display the data you have in your C# lists; this blog covers:
- Duplicate records
- Sort Ints
- Display class
- Display list of Ints
- Display list of Joined Ints
- Display list of strings
Find Duplicate Records in a C# List
Sometimes we may need to find duplicate records in our list, to do this we use a LINQ query on the list to group the records, then we do a count on the records and if greater than 1, display the records as the code shows.
Sort C# List
Sometimes we need to sort the order of a C# list, in the code below the list has numbers in a random order, using sort on the list returns the numbers in ASC order.
Display C# Class in List
It's not just strings and ints a C# list can return, in this example we return a class with two properties and create a list of pets, we then display the list of pets on the page.
Display C# List<Int>
In this example, we return a list of ints and display on the page; this is a very example of returning a list of ints.
Display C# list of Joined Ints
This example shows how we join two lists of ints, together, the first list is from another class, and we join that to another method in a class as the code below shows.
Display C# list of strings
Again this is a straightforward example of returning a list of strings, the only difference between this and a list of ints is List<string> as the code below shows.
Putting all the code together is shown below, and it shows the different classes, controller and page.
If you found this post helpful, please leave a comment below