C# List With Examples

Shahzad Aslam
2 min readFeb 11, 2024
List in C# with examples

List is related to System.Collections.Generic namespace. In List you can add, remove, delete, sort and perform lot of operation on items. List can grow automatically as size increased (Dynamically can grow). Array has fixed size. (it can not grow dynamically).

Features with list in C#:

Creating a List:

You will use System.Collections.Generic namespace for creating list. A List of specific data type can be create in the below way.

using System.Collections.Generic;
List<int> fruitprices = new List<int>();
List<string> fruitnames = new List<string>();

Adding Elements:

You can add elements to a list using the Add method.

fruitprices.Add(10);
fruitnames.Add("Apple");

Initializing a List:

You can initialize a list with elements using collection initializer.

List<int> fruitprices = new List<int> { 10, 20, 30, 40, 50 };
List<string> fruitnames = new List<string> { "Apple", "Orange", "Cherry", "Grape", "Banana" };

Accessing Elements:

You can access elements by their index, just like arrays.

int fruitprice = fruitprices[0];
string fruitname =…

--

--

Shahzad Aslam
Shahzad Aslam

Written by Shahzad Aslam

Welcome to c-sharptutorial.com. I am Shahzad Aslam. I am the founder of c-sharptutorial.com. I'm currently living near Islamabad, Pakistan.

No responses yet