// (Mono) Compile with "gmcs Main.cs" // (Mono) Run with "mono Main.exe" using System; using System.Collections.Generic; public class MainClass { public static void Main (string[] args) { // Notice: C# will autobox the in generics // -- primitives treated as first class objects BUT pass by value // -- strings are primitives too. List cow = new List(); List dog = new List(); List elephant = new List(); for(int i = 0; i < 10; i ++) { cow.Add(3 * i); dog.Add(0.25 * i); if(i % 2 == 0) elephant.Add("Even"); else elephant.Add("Odd"); } Console.WriteLine("== Generic List Printer =="); PrintArrayList(cow); PrintArrayList(dog); PrintArrayList(elephant); Console.WriteLine(); Console.WriteLine("== Generic Element Accessing =="); // "var" keyword allows you to let the compiler infer // the correct type returned by a generic function! // NOT the same as saying "Object" since "var" will specify // the narrowest type possible. var cow_at_7 = GetElement(cow, 7); var dog_at_2 = GetElement(dog, 2); var elephant_at_4 = GetElement(elephant, 4); Console.WriteLine("Cow at 7 = " + cow_at_7); Console.WriteLine("Dog at 2 = " + dog_at_2); Console.WriteLine("Elephant at 4 = " + elephant_at_4); } // C# method name convention: upper case + camel case static void PrintArrayList(List animalList) { foreach(A a in animalList) Console.Write(a + "\t"); Console.WriteLine(); } // Notice: C# generics offer native [bracket] indexing notation! static A GetElement(List what, int which) { return what[which]; } }