---
title: "Basic Concept of List in c#"  
description: "A list is the collection of data of the same datatype. It is similar to Array but easy to handle.  The syntax for the declaration of list // List   li"  
author: "Abhishek Srivasatava"  
published: 2016-09-19  
updated: 2018-03-16  
canonical: https://www.mindstick.com/blog/11207/basic-concept-of-list-in-c-sharp  
category: "c#"  
tags: ["c#", "list"]  
reading_time: 3 minutes  

---

# Basic Concept of List in c#

A list is the [collection](https://www.mindstick.com/articles/1718/collections-in-java) of data of the same datatype. It is similar to Array but easy to handle.\

The syntax for the declaration of list // List<int> list1= new List<int>();

**Different way to add members :**

1 : By using Add [keyword](https://www.mindstick.com/forum/33572/sql-inner-join-keyword)

```
          list1.Add(7);          list2.Add(8);          list3.Add(9);
```

2 : [Adding data](https://www.mindstick.com/forum/12867/adding-data-to-a-radiobuttonlist-in-a-random-order) at the time of object [creation](https://yourviews.mindstick.com/view/85506/what-was-the-causes-of-creation-of-pakistan) :

```
List<int> list1 = new List<int     
```

**Different way to [display data](https://www.mindstick.com/forum/160249/what-is-the-role-of-the-display-data-annotation-to-display-name-for-a-model-property-in-dot-net-core)**

1: By using [for loop](https://www.mindstick.com/forum/12568/android-for-loop-not-working-in-main-thread) .

We can get the number of member present in the list by the keyword Count.

**Syntax:**

```
List<int> list1 = new List<int>(new int[]{7, 8, 9 });int n = list1.count;for ( int i=0;i<n;i++)
Console.WriteLine(list[i]);
```

2* By using [foreach loop](https://www.mindstick.com/blog/298/parellel-foreach-loop) :

```
foreach (int i in list){Console.WriteLine(j);}
```

**How to clear the List?**

List can be clear by using clear keyworld.

```
syntax //  list1.clear();
```

**As list is similar to array so we can directly [copy array](https://www.mindstick.com/forum/156576/copy-array-items-into-another-array-via-javascript) to list**

```
int[] arr = new int[3];
//defined the member of array by using the for loop.
List<int> list = new List<int>(arr); // Copy to List.
```

**If we want to know the position of any member in thelist then we need to use Indexof .**

```
syntax //  int k= list.IndexOf(9);
```

it will work fine if list contain only single element for the search element i.e., list contain only one '9'.

If search element is not found in the list then it will return '-1'.

If the search element is found in the list, but more than one place, then it will return '0'.

**Inserting element in the list :**

If list contain element like 2,3,6,8.

And if we want to insert a new element in between 3 and 6 then Insert is used

```
//List<int> list1 = new List<int>(new int[]{ 2,3,6,8 });
List1.insert(2,5); //2 is the position of the element.
```

now new list will be 2,3,5,6,8

**How to Reverse the String?**\
We can Reverse the string by using string keyword.

```
//list1.Reverse();
```

**Here is the small [program](https://www.mindstick.com/blog/12337/scaling-up-your-mentorship-program) which may help you to clear above [concept](https://www.mindstick.com/blog/79/routing-concept-in-dot-net)**

```
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks; namespace ConsoleApplication1{    class Program    {        static void Main()        {            List<int> list1 = new List<int>();            list1.Add(7);            list1.Add(8);            list1.Add(9);            Console.WriteLine("List1 element displaying by using foreach loop");            foreach (int j in list1)            {              Console.WriteLine(j);            }            List<int> list2 = new List<int>(new int[]{ 2, 3, 6,8 });            Console.WriteLine("List2 element displaying by using foreach loop");            foreach (int l in list2)            {              Console.WriteLine(l);            }            int n = list1.Count;            Console.WriteLine("List1 element displaying by using for loop");            for (int i = 0; i < n; i++)            {                Console.WriteLine(list1[i]);            }            list2.Insert(2,5);            int a = list1.Count();            Console.WriteLine("List element displaying after Inserting an element ");            foreach (int z in list2)            {             Console.WriteLine(z);            }            list1.Reverse();            Console.WriteLine("List element displaying after Reversing the list ");            foreach (int m in list1)            {                Console.WriteLine(m);            }            list1.Clear();            Console.ReadLine();        }    }}
```

**\**

**Output:**

```

```

---

Original Source: https://www.mindstick.com/blog/11207/basic-concept-of-list-in-c-sharp

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
