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:

  1. Duplicate records
  2. Sort Ints
  3. Display class
  4. Display list of Ints
  5. Display list of Joined Ints
  6. 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.

Summary

If you found this post helpful, please leave a comment below

Leave a comment

*
*

Comments

helful

Comment posted by Vihaan on 20 Dec 2019 14:11

Hi Vihaan good to hear you found it helpful