List in c# RealTime Experience PART 1

A List is a strongly typed list of objects that can be accessed by index. It can be found under System.Collections.Generic namespace.


Is .NET Type-Safe?



A type error is erroneous or undesirable program behaviour caused by a discrepancy between differing data types.

Arrays do not dynamically resize. The List type in the C# language does.

List.ADD()

using System.Collections.Generic;

class Program
{
    static void Main()
    {
List<int> list = new List<int>();
list.Add(2);
list.Add(3);
list.Add(5);
list.Add(7);
    }
}

foreach (int prime in list) // Loop through List with foreach
{
    Console.WriteLine(prime);
}
C# AddRange

AddRangeadds an entire collection of elements

using System;
using System.Collections.Generic;

class Program
{
    static void Main()
    {
List<int> a = new List<int>();
a.Add(1);
a.Add(2);
a.Add(5);
a.Add(6);

// Contains: // 1 // 2 // 5 // 6

int[] b = new int[3];
b[0] = 7;
b[1] = 6;
b[2] = 7;

a.AddRange(b);

// Contains: // 1 // 2 // 5 // 6 // 7 [added] // 6 [added] // 7 [added]
foreach (int i in a)
{
    Console.WriteLine(i);
}
    }
}

Output

1
2
5
6
7
6
7

Count

Programming tip
To get the number of elements in your List, access the Count property. This is fast to access, if you avoid the Count() extension method. Count is equal to Length on arrays. See the next section for an example on using the Count property


Clear

Here we use the Clear method, along with the Count property, to erase all the elements in a List.


Insert

You can insert an element into your List at any position.
 
using System;
using System.Collections.Generic;

class Program
{
    static void Main()
    {
List<string> dogs = new List<string>(); // Example List

dogs.Add("spaniel");         // Contains: spaniel
dogs.Add("beagle");          // Contains: spaniel, beagle
dogs.Insert(1, "dalmation"); // Contains: spaniel, dalmation, beagle

foreach (string dog in dogs) // Display for verification
{
    Console.WriteLine(dog);
}
    }
}

Output

spaniel
dalmation
beagle

 

C# InsertRange

 
First, the InsertRange is closely related to the AddRange method, which allows you to add an array or other collection on the end of a List.
 
 
using System;
using System.Collections.Generic;

class Program
{
    static void Main()
    {
List<int> a = new List<int>();
a.Add(1);
a.Add(2);
a.Add(5);
a.Add(6);

// Contains: // 1 // 2 // 5 // 6

int[] b = new int[3];
b[0] = 7;
b[1] = 6;
b[2] = 7;

a.InsertRange(1, b);

// Contains: // 1 // 7 [inserted] // 6 [inserted] // 7 [inserted] // 2 // 5 // 6
foreach (int i in a)
{
    Console.WriteLine(i);
}
    }
}

Output

1
7
6
7
2
5
6
 

 

C# RemoveAt

RemoveAtremoves one element
using System;
using System.Collections.Generic;

class Program
{
    static void Main()
    {
var list = new List<string>();
list.Add("dot");
list.Add("net");
list.Add("perls");

list.RemoveAt(1);

foreach (string element in list)
    Console.WriteLine(element);
    }
}

Output

dot
perls
 

C# RemoveAll

 
RemoveAllfilters and removes elements
using System;
using System.Collections.Generic;

class Program
{
    static void Main()
    {
List<int> list = new List<int>();
list.Add(1);
list.Add(2);
list.Add(2);
list.Add(4);
list.Add(5);

// Remove all list items with value of 2. // The lambda expression is the Predicate.
list.RemoveAll(item => item == 2);

// Display results.
foreach (int i in list)
{
    Console.WriteLine(i);
}
    }
}

Output

1
4
5
 
 

Sort

Sorted letters: A through Z
Sort orders the elements in the List. For strings it will order them alphabetically. For integers or other numbers it will order them from lowest to highest. It acts upon elements depending on their type. It is also possible to provide a custom comparison method.

using System;
using System.Collections.Generic;

class Program
{
    static void Main()
    {
List<string> list = new List<string>();
list.Add("tuna");
list.Add("velvetfish");
list.Add("angler");

// Sort fish alphabetically, in ascending order (A - Z)
list.Sort();

foreach (string value in list)
{
    Console.WriteLine(value);
}
    }
}

Output

angler
tuna
velvetfish

 

Reverse

 
using System;
using System.Collections.Generic;

class Program
{
    static void Main()
    {
List<string> list = new List<string>();
list.Add("anchovy");
list.Add("barracuda");
list.Add("bass");
list.Add("viperfish");

// Reverse List in-place, no new variables required
list.Reverse();

foreach (string value in list)
{
    Console.WriteLine(value);
}
    }
}

Output

viperfish
bass
barracuda
anchovy
 

GetRange

You can get a range of elements in your List collection using the GetRange instance method. This is similar to the Take and Skip methods from LINQ. It has different syntax.
Program that gets ranges from List [C#]

using System;
using System.Collections.Generic;

class Program
{
    static void Main()
    {
	List<string> rivers = new List<string>(new string[]
	{
	    "nile",
	    "amazon",     // River 2
	    "yangtze",    // River 3
	    "mississippi",
	    "yellow"
	});

	// Get rivers 2 through 3
	List<string> range = rivers.GetRange(1, 2);
	foreach (string river in range)
	{
	    Console.WriteLine(river);
	}
    }
}

Output

amazon
yangtze

 

Comments