xxxxxxxxxx
List<string> myListOfStrings = new List<string>
{
"this",
"is",
"my",
"list"
};
xxxxxxxxxx
using System.Collections.Generic;
private List<string> list; //creates list named list of type string
list.Add(string); //adds string at last index of list
list.Count; //get length of list
list.Insert(2,"pos 2"); // Insert string "pos 2" in position 2
list[i]; //get item from index i
xxxxxxxxxx
var list = new List<T>();
// uses list.Method();
public void Add (T item);
public bool Remove (T item);
public int RemoveAll (Predicate<T> match);
public void RemoveAt (int index);
public void RemoveRange (int index, int count);
public void Reverse ();
public void Reverse (int index, int count);
public void Sort (Comparison<T> comparison);
public void Sort (int index, int count, System.Collections.Generic.IComparer<T>? comparer);
public void Sort ();
public T[] ToArray ();
public void AddRange (System.Collections.Generic.IEnumerable<T> collection);
public System.Collections.ObjectModel.ReadOnlyCollection<T> AsReadOnly ();
public int BinarySearch (T item);
public void Clear ();
public bool Contains (T item);
public System.Collections.Generic.List<TOutput> ConvertAll<TOutput> (Converter<T,TOutput> converter);
public void CopyTo (T[] array, int arrayIndex);
public void CopyTo (int index, T[] array, int arrayIndex, int count);
public void CopyTo (T[] array);
public int EnsureCapacity (int capacity);
public bool Exists (Predicate<T> match);
public T? Find (Predicate<T> match);
public System.Collections.Generic.List<T> FindAll (Predicate<T> match);
public int FindIndex (int startIndex, int count, Predicate<T> match);
public int FindIndex (Predicate<T> match);
public int FindIndex (int startIndex, Predicate<T> match);
public T? FindLast (Predicate<T> match);
public int FindLastIndex (Predicate<T> match);
public void ForEach (Action<T> action);
public System.Collections.Generic.List<T>.Enumerator GetEnumerator ();
public System.Collections.Generic.List<T> GetRange (int index, int count);
public int IndexOf (T item, int index);
public int IndexOf (T item, int index, int count);
public int IndexOf (T item);
public void Insert (int index, T item);
public void InsertRange (int index, System.Collections.Generic.IEnumerable<T> collection);
public int LastIndexOf (T item);
public int LastIndexOf (T item, int index);
public int LastIndexOf (T item, int index, int count);
public void TrimExcess ();
public bool TrueForAll (Predicate<T> match);
xxxxxxxxxx
// List with default capacity
List<Int16> list = new List<Int16>();
// List with capacity = 5
List<string> authors = new List<string>(5);
string[] animals = { "Cow", "Camel", "Elephant" };
List<string> animalsList = new List<string>(animals);
xxxxxxxxxx
List<int> numbers = new List<int>();
// Add/Remove Elements from a List
numbers.Add(1);
numbers.Remove(1);
// Access Elements
int firstNumber = numbers[0];
Console.WriteLine(firstNumber); // Output = 1
// Collection of Elements:
List<string> colors = new List<string> { "coal", "emerald", "sapphire" };
// Generic Uses
int count = numbers.Count;
numbers.Clear();
xxxxxxxxxx
using System;
using System.Collections.Generic;
public class Example
{
public static void Main(string[] args)
{
// Create a list of strings
var countries = new List<string>();
countries.Add("India");
countries.Add("Australia");
countries.Add("Japan");
countries.Add("Canada");
countries.Add("Mexico");
// Iterate list element using foreach loop
foreach (var country in countries)
{
Console.WriteLine(country);
}
}
}
xxxxxxxxxx
List allows you to store different types of data in a Node/List
type structure, example for List<string>:
("word") --> ("word") --> ("word") --> null
every List item contains the value and the 'address' to the next list item.
Add objects by using .Add() --> list.Add("word")
output:
("word") --> ("word") --> ("word") --> ("word") --> null
xxxxxxxxxx
List<string> names = new List<string>();List<Object> someObjects = new List<Object>();
xxxxxxxxxx
using System;
using System.Collections.Generic;
namespace main {
class Program {
private static void Main(string[] args) {
// syntax: List<type> name = new List<type>();
List<int> integerList = new List<int>();
for (int i = 0; i < 10; i++) {
// add item to list
integerList.Add(i);
}
for (int i = 0; i < 10; i++) {
// print list item
Console.WriteLine(integerList[i]);
}
}
}
}