Add value to Generic List and display the array list using array indexing
using System;
using System.Collections.Generic;
class MainClass {
public static void Main() {
List<char> lst = new List<char>();
Console.WriteLine("Initial number of elements: " +
lst.Count);
Console.WriteLine();
Console.WriteLine("Adding 6 elements");
lst.Add("C");
lst.Add("A");
lst.Add("E");
lst.Add("B");
lst.Add("D");
lst.Add("F");
Console.WriteLine("Number of elements: " +
lst.Count);
Console.Write("Current contents: ");
for(int i=0; i < lst.Count; i++)
Console.Write(lst[i] + " ");
Console.WriteLine("\n");
}
}
Initial number of elements: 0
Adding 6 elements
Number of elements: 6
Current contents: C A E B D F
Change contents in a generic List using array indexing
using System;
using System.Collections.Generic;
class MainClass {
public static void Main() {
List<char> lst = new List<char>();
Console.WriteLine("Initial number of elements: " +
lst.Count);
Console.WriteLine();
Console.WriteLine("Adding 6 elements");
lst.Add("C");
lst.Add("A");
lst.Add("E");
lst.Add("B");
lst.Add("D");
lst.Add("F");
Console.WriteLine("Number of elements: " +
lst.Count);
Console.WriteLine("Change first three elements");
lst[0] = "X";
lst[1] = "Y";
lst[2] = "Z";
Console.Write("Contents: ");
foreach(char c in lst)
Console.Write(c + " ");
Console.WriteLine();
}
}
Initial number of elements: 0
Adding 6 elements
Number of elements: 6
Change first three elements
Contents: X Y Z B D F
Create Reverse Iterator
using System;
using System.Collections.Generic;
public class MainClass
{
static void Main() {
List<int> intList = new List<int>();
intList.Add( 1 );
intList.Add( 2 );
intList.Add( 3 );
intList.Add( 4 );
foreach( int n in CreateReverseIterator(intList) ) {
Console.WriteLine( n );
}
}
static IEnumerable<T> CreateReverseIterator<T>( IList<T> list ) {
int count = list.Count;
for( int i = count-1; i >= 0; --i ) {
yield return list[i];
}
}
}
4
3
2
1
List Capacity and Element Count
using System;
using System.Collections.Generic;
class MainClass {
public static void Main() {
List<char> lst = new List<char>();
Console.WriteLine("Initial number of elements: " +
lst.Count);
Console.WriteLine();
Console.WriteLine("Adding 6 elements");
lst.Add("C");
lst.Add("A");
lst.Add("E");
lst.Add("B");
lst.Add("D");
lst.Add("F");
Console.WriteLine("Number of elements: " +
lst.Count);
Console.WriteLine("Adding 20 more elements");
// Add enough elements to force lst to grow.
for(int i=0; i < 20; i++)
lst.Add((char)("a" + i));
Console.WriteLine("Current capacity: " +
lst.Capacity);
Console.WriteLine("Number of elements after adding 20: " +
lst.Count);
Console.Write("Contents: ");
foreach(char c in lst)
Console.Write(c + " ");
Console.WriteLine("\n");
}
}
Initial number of elements: 0
Adding 6 elements
Number of elements: 6
Adding 20 more elements
Current capacity: 32
Number of elements after adding 20: 26
Contents: C A E B D F a b c d e f g h i j k l m n o p q r s t
Remove elements from generic List
using System;
using System.Collections.Generic;
class MainClass {
public static void Main() {
List<char> lst = new List<char>();
Console.WriteLine("Initial number of elements: " +
lst.Count);
Console.WriteLine();
Console.WriteLine("Adding 6 elements");
lst.Add("C");
lst.Add("A");
lst.Add("E");
lst.Add("B");
lst.Add("D");
lst.Add("F");
Console.WriteLine("Number of elements: " +
lst.Count);
Console.WriteLine("Removing 2 elements");
// Remove elements from the array list.
lst.Remove("F");
lst.Remove("A");
Console.WriteLine("Number of elements: " +
lst.Count);
}
}
Initial number of elements: 0
Adding 6 elements
Number of elements: 6
Removing 2 elements
Number of elements: 4
Store user-defined Objects in a List<T> collection
using System;
using System.Collections.Generic;
class Product {
string name;
double cost;
int onhand;
public Product(string n, double c, int h) {
name = n;
cost = c;
onhand = h;
}
public override string ToString() {
return
String.Format("{0,-10}Cost: {1,6:C} On hand: {2}",
name, cost, onhand);
}
}
class MainClass {
public static void Main() {
List<Product> inv = new List<Product>();
// Add elements to the list
inv.Add(new Product("A", 5.9, 3));
inv.Add(new Product("B", 8.2, 2));
inv.Add(new Product("C", 3.5, 4));
inv.Add(new Product("D", 1.8, 8));
Console.WriteLine("Product list:");
foreach(Product i in inv) {
Console.WriteLine(" " + i);
}
}
}
Product list:
A Cost: $5.90 On hand: 3
B Cost: $8.20 On hand: 2
C Cost: $3.50 On hand: 4
D Cost: $1.80 On hand: 8
Use foreach loop to display the generic list
using System;
using System.Collections.Generic;
class MainClass {
public static void Main() {
List<char> lst = new List<char>();
Console.WriteLine("Initial number of elements: " +
lst.Count);
Console.WriteLine();
Console.WriteLine("Adding 6 elements");
lst.Add("C");
lst.Add("A");
lst.Add("E");
lst.Add("B");
lst.Add("D");
lst.Add("F");
Console.WriteLine("Number of elements: " +
lst.Count);
// Use foreach loop to display the list.
Console.Write("Contents: ");
foreach(char c in lst)
Console.Write(c + " ");
Console.WriteLine("\n");
}
}
Initial number of elements: 0
Adding 6 elements
Number of elements: 6
Contents: C A E B D F
Use generic list to store your own class
using System.Collections.Generic;
using System;
class MainClass
{
public static void Main(string[] args)
{
List<MyClass> list = new List<MyClass>();
list.Add(new MyClass());
MyClass ass2 = list[0];
Console.WriteLine("\nFound in list: {0}", ass2);
}
}
class MyClass {
public override string ToString(){
return "my class";
}
}
Found in list: my class