List in C# is a collection of strongly typed objects. These objects can be easily accessed using their respective index. Index calling gives the flexibility to sort, search, and modify lists if required.

In simple, List in C# is the generic version of the ArrayList. This ArrayList comes under System.Collection.Generic namespace.

Syntax of the list in C#: 

To create a list in C#, the following syntax is used:  List<T>

What Is the ‘T’ in List<T>? 

In the list, the letter T stands for the type of object. This type is passed as a parameter during runtime. As we said, T means the Object Type, so, if you want to create a list that has nothing but strings then you have to use object type- string in place of T. 

List<T> Class Properties and Methods: 

  • A List may look like an array, but it's different from the Arrays because Arrays cannot be resized, whereas List<T> can be.
  • A null value is treated as valid input in List<T> as reference types. Also, duplicate values are termed valid. 
  • The list has a dynamic size which means when the count becomes equal to the maximum capacity, it automatically increases the capacity. This increment is done by reallocating the internal array.
  • Before the addition of the new element, the existing element gets copied to the new array. 
  • By implementing the List<T> generic interface, the List<T> class becomes the generic equivalent of the class ArrayList.
  • Accessing the elements starts with zero indexes because the List<T> class is not sorted by default. 
  • List size can be increased up to 2 billion (only when your system works on 64-bit or higher) to store large List<T> objects. 

How to Create List in C#?

Being a generic collection List<T> needs to be specified with the required type parameter to store a particular type of data.  

Here's how you can create a List<T>

C_Sharp_List_1

From the above snippet List<string> AuthorList= new List<string>(); is the particular syntax to create a list of type strings. Likewise, you can create any list of your desired data type. In case you want to create an integer list, then simply replace object type- string with object type- int. 

Unleash Your Career as a Full Stack Developer!

Full Stack Developer - MERN StackEXPLORE COURSE
Unleash Your Career as a Full Stack Developer!

How to Add Items to a C# List?

To add items and elements in a C# list, you need to use the Add() method. If you want to add elements of the custom classes, then you can do so by using the collection-initializer syntax.

Check this code snippet to have a clear idea- 

C_Sharp_List_2

How to Read a C# List Items?

The List<T> is a generic collection; to read each of its elements, you need to use a loop. You can use the foreach loop because it's simple and quick to go through all the items present in a loop one by one and read them. 

Check the below code snippet for a clear idea- 

C_Sharp_List_3

How to Use C# List Properties? 

Properties in C# List can be used as per the requirement. Following are List class properties:  

  • Capacity – This determines the number of elements List<T> can contain

Note- The default capacity of List<T> is 0.

  • Count – This determines the number of elements present in a List<T>

Unleash Your Career as a Full Stack Developer!

Full Stack Developer - MERN StackEXPLORE COURSE
Unleash Your Career as a Full Stack Developer!

How to Insert Elements at a Position in a C# List? 

To insert elements at your desired position in a List, you can use the following syntax: 

public void Insert (int index, T item);

C_Sharp_List_4

To add elements at the end, use the method List.Add() method. Method List.AddRange() adds a collection of objects to the end of a given List<T>.

How to Remove Items From a C# List?

The Remove() technique will easily eliminate the initial appearance of a particular object from a list. The Remove technique assumes an element as its parameter. 

The RemoveAt technique eliminates an element at the specified zero-based index.

How to Find an Element in a C# List?

The Enumerable.Contains() technique offers an easy and neat means to deduce if a sequence comprises the desired component or not. 

The alternative solution is to utilize the List.IndexOf() technique that returns the index of the initial appearance of the desired component in this list and will display -1 in case there is no such component.

C_Sharp_List_5

How to Sort a C# List Elements? 

List<T>.Sort() process is utilized to order the components or a part of the components in the List<T> employing either the particular or default IComparer<T> enactment or a given Comparison<T> envoy to correlate list components.

C_Sharp_List_6.

Output: 

C_Sharp_List_7.

How to Reverse a C# List Elements? 

To establish a rearward copy of the actual list, one can utilize the Enumerable.Reverse() procedure. This method will establish a fresh sequence with components in a reverse arrangement without amending the list stored in it.

An alternative method to overturn the order of the components in the given list can utilize the List<T>.Reverse() procedure. List<T>.Reverse() employs an integrated algorithm. That implies that the transformation arises without utilizing any secondary list by overwriting the previous components of the provided list.

C_Sharp_List_8.

Output: 

C_Sharp_List_9

Unleash Your Career as a Full Stack Developer!

Full Stack Developer - MERN StackEXPLORE COURSE
Unleash Your Career as a Full Stack Developer!

How to Search a C# List Elements?

The List.FindAll() technique gives back a list comprising every component that conforms to the given predicate. One can employ it to examine the presence of a component in the provided list. 

C_Sharp_List_10.

Conclusion 

Found this topic interesting? Well, C# contains many such interesting topics to learn and implement like other programming languages. If you want to master this language skill to improve your CV and improve your job prospects, you can check out our Full Stack Developer - MERN Stack course which is among our most demanding courses, with a complete step-by-step overview from top educators.

Simplilearn also offers SkillUp courses in several domains, from data science and business analytics to software development, AI, and machine learning are one of the best available picks currently. You will get complete access to the course material and the program provides 24X7 expert support for all your doubts. 

Our Software Development Courses Duration And Fees

Software Development Course typically range from a few weeks to several months, with fees varying based on program and institution.

Program NameDurationFees
Caltech Coding Bootcamp

Cohort Starts: 24 Feb, 2025

6 Months$ 8,000
Automation Test Engineer Masters Program

Cohort Starts: 8 Jan, 2025

8 months$ 1,499
Full Stack Java Developer Masters Program

Cohort Starts: 8 Jan, 2025

7 months$ 1,449
Full Stack (MERN Stack) Developer Masters Program

Cohort Starts: 8 Jan, 2025

6 Months$ 1,449