Advertisement
Constructors | Description |
---|---|
List<T>() | This constructor of List<T> creates an empty List<T> and has the default initial capacity. The capacity of a List<T> is the number of elements it can hold, as the new elements are added to the List<T>, its capacity is automatically increased. |
List<T>(IEnumerable<T>) | This constructor creates a List<T> initialized with the elements of an IEnumerable<T> collection. |
List<T>(int capacity) | This constructor initializes created a List<T> that is empty and has the specified initial capacity.. |
Properties | Description |
---|---|
Capacity | This property is used to get or set the total number of elements a List<T> can hold. |
Count | This property gives total number of elements in a List<T>. |
IList.IsFixedSize | This property determines whether a List<T> has a fixed size or not. |
IList.IsReadOnly | This property determines whether a List<T> is read-only or not. |
IsSynchronzied | This property determines whether a List<T> is thread-safe or not. |
Item[int index] | This property gets or sets an item of List<T> at a specified index. To use this property, you should not type Item, but you only need to specify the specific key within a pair of square brackets [ ]. |
IList.Item[int index] | This property gets or sets an item of List<T> at a specified index. |
Methods | Description |
---|---|
int Add(T object) | This method adds the specified object at the end of a List<T> and returns its index. |
bool Contains(T object) | This method checks if List<T> contains a specific object. |
void Clear() | This method removes all of the elements from the List<T>. |
List<T>.Enumerator GetEnumerator() | This method returns to enumerate the entire List<T>. |
int IndexOf(T object) | This method searches for the specified object and returns the zero-based index of the first occurrence within the entire List<T>. |
int LastIndexOf(T o) | This method searches for the specified object and returns the zero-based index of the last occurrence within the entire List<T>. |
bool Remove(T object) | This method removes the first occurrence of an object from the List<T>, if it is present. |
bool RemoveAt(int index) | This method removes an element at a specified index in a List<T>. |
T[] ToArray() | This method returns an array of type of List<T>, containing all the elements of the List<T>. |
void Sort() | This method sorts a List<T>. |
void Reverse() | This method reverses the order of the elements of a List<T>. |
Advertisement
//C# Example of List<T>
using System;
using System.Collections.Generic;
class ListEx
{
public static void Main(String[] ar)
{
//Creating a List<T> of type List<String> to hold String objects.
List<String> list = new List<String>();
//Printing the initial capacity of a List<T>
Console.WriteLine("Initial capacity of List<String> = "+ list.Capacity);
//Adding String objects to List<String> by Add() method
list.Add("C");
list.Add("A");
list.Add("1");
list.Add("F");
list.Add("Bye");
list.Add("Hello");
//Printing the elements of a List<String>, using for loop
Console.Write("List<String> after adding String objects: ");
for(int i=0;i<list.Count;i++)
Console.Write(list[i]+ " ");
//Printing the total number of elements in a List<String>.
Console.WriteLine("\nSize of List<String> = "+ list.Count);
//Removing an element at the 4th index of List<String>
Console.WriteLine("Removing an element at 4th Index of List<String>");
list.RemoveAt(4);
//Removing an element "1" of the List<String>
Console.WriteLine("Removing an element 1 of List<String>");
list.Remove("1");
//Printing the elements of a List<String>, using foreach loop
Console.Write("An updated List<String>: ");
for(int i = 0; i<list.Count; i++)
Console.Write(list[i] + " ");
//Printing the total number of elements in a List<String>.
Console.WriteLine("\nSize of List<String> = "+ list.Count);
//Printing the current capacity of a List<String>
Console.WriteLine("Current capacity of List<String> = "+ list.Capacity);
}
}
Initial capacity of List<String> = 0
List<String> after adding String objects: C A 1 F Bye Hello
Size of List<String> = 6
Removing an element at 4th Index of List<String>
Removing an element 1 of List<String>
An updated List<String>: C A F Hello
Size of List<String> = 4
//C# Iterating over the elemets of List<T> using foreach loop
using System;
using System.Collections.Generic;
class ListEx
{
public static void Main(String[] ar)
{
//Creating a List<T> of type List<String> to hold String objects.
List<String> list = new List<String>();
//Printing the initial capacity of a List<T>
Console.WriteLine("Initial capacity of List<String> = "+ list.Capacity);
//Adding String objects to List<String> by Add() method
list.Add("Hi");
list.Add("Hello");
list.Add("Bonjour");
list.Add("Salut");
list.Add("Buna");
//Printing the elements of a List<String>, using foreach loop
Console.WriteLine("The contents of List<String> objects: ");
foreach(String ob in list)
Console.WriteLine(ob + " ");
//Printing the total number of elements in a List<String>.
Console.WriteLine("Size of List<String> = "+ list.Count);
}
}
Initial capacity of List<String> = 0
The contents of List<String> objects:
Hi
Hello
Bonjour
Salut
Buna
Size of List<String> = 5
//C# Example of creating a new List<T> created from an existing one
using System;
using System.Collections.Generic;
class ListEx
{
public static void Main(String[] ar)
{
//Creating a List<T> of type List<int> to hold int
//which are implicitly converted to objects.
List<int> list1 = new List<int>();
//Adding elements to a List<int> by Add() method
list1.Add(4);
list1.Add(1);
list1.Add(5);
list1.Add(2);
list1.Add(3);
//Printing the elements of a List<int>, using for loop with Count property
Console.WriteLine("List<int> after adding objects: ");
for(int i = 0; i<list1.Count; i++)
Console.WriteLine(list1[i] + " ");
//Printing the total number of elements in a List<int>.
Console.WriteLine("Size of List<int> = "+ list1.Count);
Console.WriteLine("Creating a new List<int> created from an existing one");
//Calling a constructor of List<int>
//Which creates a new List<int>, initialized from an existing one
List<int> list2= new List<int>(list1);
//Printing the elements of a List<int>, using foreach loop
Console.WriteLine("Contents of the new List<int>: ");
for(int i = 0; i<list2.Count; i++)
Console.WriteLine(list2[i] + " ");
//Printing the total number of elements in a List<int>.
Console.WriteLine("Size of the new List<int> = "+ list2.Count);
}
}
List<int> after adding objects:
4
1
5
2
3
Size of List<int> = 5
Creating a new List<int> created from an existing one
Contents of the new List<int>:
4
1
5
2
3
Size of the new List<int> = 5
//C# Converting a List<T> to an array of the T type of List<T> by callin the ToArray() method.
using System;
using System.Collections.Generic;
class ListEx
{
public static void Main(String[] ar)
{
//Creating a List<T> of type List<char> to hold characters
List<char> list1 = new List<char>();
//Adding char elements to a List<char> by Add() method
list1.Add('x');
list1.Add('e');
list1.Add('a');
list1.Add('f');
list1.Add('b');
//Printing the elements of a List<char>, using foreach loop
Console.WriteLine("List<char> after adding objects: ");
for(int i = 0; i<list1.Count; i++)
Console.WriteLine(list1[i]);
//Printing the total number of elements in a List<char>.
Console.WriteLine("Size of List<char> = "+ list1.Count);
//By calling the ToArray() method
//which converts a List<char> to an char array.
char[] obArr = list1.ToArray();
Console.WriteLine("Creating an array of type of List<char> created from List<char> - ");
//Iterating over the elements of Object array, using for loop
Console.WriteLine("The elements of Object array: ");
foreach(char ch in obArr)
Console.WriteLine(ch);
}
}
List<char> after adding objects:
x
e
a
f
b
Size of List&;t;char> = 5
Creating an array of type of List<char> created from List<char> -
The elements of Object array:
x
e
a
f
b
//C# Using the Sort() method of List<T>
using System;
using System.Collections.Generic;
class ListEx
{
public static void Main(String[] ar)
{
//Creating a List<T> of type List<int> to hold int
//which are implicitly converted to objects.
List<int> list1 = new List<int>();
//Adding elements to a List<int> by Add() method
list1.Add(4);
list1.Add(1);
list1.Add(5);
list1.Add(2);
list1.Add(3);
.
//Printing the elements of a List<int>, using foreach loop
//We could used the for loop with Count property
Console.WriteLine("List<int> after adding objects: ");
foreach(int element in list1)
Console.WriteLine(element);
//Printing the total number of elements in a List<int>.
Console.WriteLine("Size of List<int> = "+ list1.Count);
//Sorting the elements of List<int> by calling its ToSort() method
list1.Sort();
Console.WriteLine("Sorted List<int>: ");
//Iterating over the elements of sorted List<T>, using foreach loop
//We could've used the for loop as well.
foreach(int element in list1)
Console.WriteLine(element);
}
}
<code>List<int> after adding objects: 4 1 5 2 3 Size of List<int> = 5 Sorted List<int>: 1 2 3 4 5
//C# Using the Reverse() method of List<T>
using System;
using System.Collections.Generic;
class ListEx
{
public static void Main(String[] ar)
{
//Creating a List<T> of type List<String> to hold String objects
List<String> list1 = new List<String>();
//Adding String objects to a List<String> by calling its Add() method
list1.Add("4");
list1.Add("A");
list1.Add("x");
list1.Add("E");
list1.Add("r");
//Printing the elements of a List<String>, using foreach loop
//We could used the for loop with Count property
Console.WriteLine("List<String> after adding objects: ");
foreach(String element in list1)
Console.WriteLine(element);
//Printing the total number of elements in a List<String>.
Console.WriteLine("Size of List<String> = "+ list1.Count);
//Reversing the order of a List<String> by calling its Reverse() method
list1.Reverse();
Console.WriteLine("Reversed List<String>: ");
//Iterating over the elements of our reversed List<String>, using foreach loop
foreach(String element in list1)
Console.WriteLine(element);
}
}
List<String> after adding objects:
4
A
x
E
r
Size of List<String> = 5
Reversed List<String>:
r
E
x
A
4
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement