How to create C# List<T> generics
Tags: csharp, dotnet, programming, tutorial
Published: 2021-02-05 10:22:06

<!-- wp:heading -->
<h2>C# List&lt;T> generics</h2>
<!-- /wp:heading -->

<!-- wp:list -->
<ul><li>C# List&lt;T> is a strongly typed collection for creating a list of objects of type &lt;T>. </li><li>It is similar to ArrayList, we can add, remove, search, sort, manipulate list-objects.</li><li>In C#, ArrayList is a non-generic collection whereas List&lt;T> is a generic collection.</li><li>List&lt;T> has high performance than ArrayList and type-safe. </li><li>List&lt;T> has similar methods and properties as the ArrayList.</li></ul>
<!-- /wp:list -->

<!-- wp:heading -->
<h2>Creating a C# List&lt;T></h2>
<!-- /wp:heading -->

<!-- wp:list -->
<ul><li>List&lt;T&gt; is created by using a new operator either it can be empty capacity or specify the size.</li></ul>
<!-- /wp:list -->

<!-- wp:heading {"level":3} -->
<h3>Syntax:</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p><code>List&lt;T&gt; &lt;list_name&gt; = new List&lt;T&gt;(&lt;capacity&gt;);</code></p>
<!-- /wp:paragraph -->

<!-- wp:list -->
<ul><li><code>&lt;T&gt;</code> is the type of object to be created like <code>int, float, double</code> etc.</li><li><code>&lt;list_name&gt;</code> name of the list.</li><li><code>&lt;capacity&gt;</code> is the size of the list which is optional.</li></ul>
<!-- /wp:list -->

<!-- wp:heading {"level":3} -->
<h3>Eg:</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p><code>List&lt;string&gt; List1 = new List&lt;string&gt;();</code></p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2>Adding object to List&lt;T&gt;</h2>
<!-- /wp:heading -->

<!-- wp:list -->
<ul><li>Add(T obj) method will add the element type &lt;T&gt; in the list on by one.</li></ul>
<!-- /wp:list -->

<!-- wp:heading {"level":3} -->
<h3>Eg:</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p><code>List1.Add(“Element1”);</code></p>
<!-- /wp:paragraph -->

<!-- wp:list -->
<ul><li><code>Insert(int index, T obj)</code> method will add the element at the specified index.</li></ul>
<!-- /wp:list -->

<!-- wp:heading {"level":3} -->
<h3>Eg:</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p><code>List1.Insert(1,”Element1”);</code></p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2>Removing object to List&lt;T&gt;</h2>
<!-- /wp:heading -->

<!-- wp:list -->
<ul><li><code>Remove(T obj)</code> method will remove the element in the list.</li></ul>
<!-- /wp:list -->

<!-- wp:heading {"level":3} -->
<h3>Eg:</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p><code>List1.Remove(“Element1”);</code></p>
<!-- /wp:paragraph -->

<!-- wp:list -->
<ul><li><code>RemoveAt(int index)</code> Removes the element at the specified index.</li></ul>
<!-- /wp:list -->

<!-- wp:heading {"level":3} -->
<h3>Eg:</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p><code>List1.RemoveAt(1);</code></p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2>Get the size of List&lt;T&gt;:</h2>
<!-- /wp:heading -->

<!-- wp:list -->
<ul><li><code>Count</code> property is used to get the size of the list.</li></ul>
<!-- /wp:list -->

<!-- wp:heading {"level":3} -->
<h3>Eg:</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p><code>List1.Count();</code></p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2>To sort the List&lt;T&gt;:</h2>
<!-- /wp:heading -->

<!-- wp:list -->
<ul><li>Use the <code>Sort()</code> method to sort the array in ascending order.</li></ul>
<!-- /wp:list -->

<!-- wp:heading {"level":3} -->
<h3>Eg:</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p><code>List1.Sort();</code></p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2>Accessing the elements in the List&lt;T&gt;</h2>
<!-- /wp:heading -->

<!-- wp:list -->
<ul><li>The index number can be used to access the elements like an array.</li></ul>
<!-- /wp:list -->

<!-- wp:heading {"level":3} -->
<h3>Eg:</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p><code>List1[0] // accessing element at index 0;</code></p>
<!-- /wp:paragraph -->

<!-- wp:list -->
<ul><li>To iterate over the List&lt;T&gt;, use foreach function.</li></ul>
<!-- /wp:list -->

<!-- wp:heading {"level":3} -->
<h3>Eg:</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p><code>foreach(string str in List1)</code></p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><code>{</code></p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><code>Console.WriteLine(str);</code></p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><code>}</code></p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":3} -->
<h3>Example:</h3>
<!-- /wp:heading -->

```c#
using System;
using System.Collections.Generic;
					
public class Program
{
	public static void Main()
	{
		//create new List<T> of string type using new operator.
		List<string> list1=new List<string>();
		
		//Adding elements using Add(T) method.
		list1.Add("Element1");
		list1.Add("Element2");
		list1.Add("Element3");
		list1.Add("Element4");
		list1.Add("Element5");
		
		
		Console.WriteLine("Printing elements in list1");
		Console.WriteLine("Size of list1: "+list1.Count);//prinitng size of list1 using count
			
		//printing list1 elements using foreach
		foreach(string str in list1)
		{
			Console.WriteLine(str);
		}
		
		//removing 'Element1' 'Element2' using Remove(T) method.
		list1.Remove("Element1");
		list1.Remove("Element2");
		
		Console.WriteLine();
		
		Console.WriteLine("Printing elements in list1 after removing Element1, Element2");
		Console.WriteLine("Size of list1: "+list1.Count);//prinitng size of list1 using count
		
		//printing list1 elements using foreach
		foreach(string str in list1)
		{
			Console.WriteLine(str);
		}
		
		list1.Insert(2,"Element1");//inserting 'Element1' in index 2
		list1.Insert(4,"Element2");//inserting 'Element2' in index 4
		
		Console.WriteLine();
		
		Console.WriteLine("Printing elements in list1 after inserting Element1, Element2");
		Console.WriteLine("Size of list1: "+list1.Count);//prinitng size of list1 using count
		//printing list1 elements using foreach
		foreach(string str in list1)
		{
			Console.WriteLine(str);
		}
		
		list1.Sort();//sorting list1
		
		Console.WriteLine();
		
		Console.WriteLine("Printing elements in list1 after sorting");
		//printing list1 elements using foreach
		foreach(string str in list1)
		{
			Console.WriteLine(str);
		}
		
	}
}

```

<!-- wp:heading {"level":3} -->
<h3>Output:</h3>
<!-- /wp:heading -->

<pre class="hljs" style="display: block; overflow-x: auto; padding: 0.5em; color: rgb(51, 51, 51); background: rgb(248, 248, 248);">Printing elements in list1
Size of list1: <span class="hljs-number" style="color: teal;">5</span>
Element1
Element2
Element3
Element4
Element5

Printing elements in list1 after removing Element1, Element2
Size of list1: <span class="hljs-number" style="color: teal;">3</span>
Element3
Element4
Element5

Printing elements in list1 after inserting Element1, Element2
Size of list1: <span class="hljs-number" style="color: teal;">5</span>
Element3
Element4
Element1
Element5
Element2

Printing elements in list1 after sorting
Element1
Element2
Element3
Element4
Element5</pre>

<!-- wp:paragraph -->
<p>It is recommended to use List&lt;T&gt; generic over ArrayList since it is type-safe, high performance.&nbsp;</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>To more refer:<a href="https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.list-1?view=net-5.0#remarks">https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.list-1?view=net-5.0#remarks</a></p>
<!-- /wp:paragraph -->

<!-- wp:separator -->
<hr class="wp-block-separator"/>
<!-- /wp:separator -->

<!-- wp:paragraph -->
<p>For my other programming archives -> <a href="https://developersdoors.xyz/category/programming/">Go</a>.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>If any suggestions please comment about it.</p>
<!-- /wp:paragraph -->