Home > DeveloperSection > Blogs > Basic Concept of List in c#

Basic Concept of List in c#

C# C#  list 
0 Comment(s)
 204  View(s)
Rate this:

Basic Concept of List in c#

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<int>   list1= new List<int>();

Different way to add members :

1 : By using Add keyword


2 : Adding data at the time of object creation :

List<int> list1 = new List<int      

 Different way to display data

1: By using for loop .

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


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 :

foreach (int i in list)

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 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.


Here is the small program which may help you to clear above concept

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>();
            Console.WriteLine("List1 element displaying by using foreach loop");
            foreach (int j in list1)
            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)
            int n = list1.Count;
            Console.WriteLine("List1 element displaying by using for loop");
            for (int i = 0; i < n; i++)
            int a = list1.Count();
            Console.WriteLine("List element displaying after Inserting an element ");
            foreach (int z in list2)
            Console.WriteLine("List element displaying after Reversing the list ");
            foreach (int m in list1)

Don't want to miss updates? Please click the below button!

Follow MindStick