How To Create A List In C#?

watch_later Saturday, April 1, 2023

Creating a list in C# is an essential part of programming. A list is a data structure that can store a collection of items of the same data type. In C#, the List<T> class is used to create a list. In this article, we will discuss how to create a list in C# and perform basic operations on it.

How To Create A List In C#?
In my previous article, I explained how to convert a datatable to CSV in c#, read a CSV file in c#, image to text conversion in c#, dependency injection c#, and read large text file batch wise using c# that you might like to read.

Step 1: Create a new project in Visual Studio

First, you need to create a new project in Visual Studio. Open Visual Studio, click on "File" and then "New Project." Choose "Console App (.NET Core)" and give your project a name.

Step 2: Import the List namespace

Before you can use the List class, you need to import the System.Collections.Generic namespace. Add the following line of code at the top of your file:

using System.Collections.Generic;

Step 3: Create a new list

To create a new list, you need to declare a variable of type List<T>. For example, to create a list of integers, you would declare a variable like this:

List<intnumbers = new List<int>();

This creates an empty list of integers called "numbers."

Step 4: Add items to the list

To add items to the list, you can use the Add method. For example, to add the numbers 42, 66, 78, and 96 to the list, you would write:

numbers.Add(42);
numbers.Add(66);
numbers.Add(78);
numbers.Add(96);

You can add as many items to the list as you want.

Step 5: Access items in the list

To access an item in the list, you can use the index operator []. For example, to access the first item in the list, you would write:

int firstItem = numbers[0];

This would assign the value of the first item in the list to the variable "firstItem."

Step 6: Remove items from the list

To remove an item from the list, you can use the Remove method. For example, to remove the first item from the list, you would write:

numbers.Remove(numbers[0]);

This would remove the first item from the list.

Step 7: Count the number of items on the list

To count the number of items in the list, you can use the Count property. For example, to get the number of items in the "numbers" list, you would write:

int count = numbers.Count;

This would assign the number of items in the list to the variable "count."

Step 8: Iterate over the items in the list

To iterate over the items in the list, you can use a foreach loop. For example, to print out all the items in the "numbers" list, you would write:

foreach (int number in numbers)
{
    Console.WriteLine(number);
}

This would print out all the items in the list, one by one.

Summary

Creating a list in C# is a simple process that involves declaring a variable of type List<T>, adding items to the list using the Add method, accessing items in the list using the index operator [], and performing other operations using various methods and properties. With these basic operations, you can create powerful programs that manipulate lists of data in many different ways.

Codingvila provides articles and blogs on web and software development for beginners as well as free Academic projects for final year students in Asp.Net, MVC, C#, Vb.Net, SQL Server, Angular Js, Android, PHP, Java, Python, Desktop Software Application and etc.



sentiment_satisfied Emoticon