public void Run() { int a, b; char c, d; a = 10; b = 20; c = 'I'; d = 'V'; //display values before swap: Console.WriteLine("Int values before calling swap:"); Console.WriteLine("a = {0}, b = {1}", a, b); Console.WriteLine("Char values before calling swap:"); Console.WriteLine("c = {0}, d = {1}", c, d); //call swap - why nsGenericMethod needed ? GenericMethod.Swap <int>(ref a, ref b); GenericMethod.Swap <char>(ref c, ref d); //display values after swap: Console.WriteLine("Int values after calling swap:"); Console.WriteLine("a = {0}, b = {1}", a, b); Console.WriteLine("Char values after calling swap:"); Console.WriteLine("c = {0}, d = {1}", c, d); Console.WriteLine("\n"); }
protected void ECButton_Click(object sender, EventArgs e) { double x = 25, y = 34; Label.Text = "x = " + x + ", y = " + y; GenericMethod.Swap(ref x, ref y); Label.Text += "<br>x = " + x + ", y = " + y; Label.Text += "<br><br>" + GenericMethod.Compare(x, y); }
/// Test the generic stack public static void TestGenericStack() { // Create stack int size = 10; GenericStack <double> stack = new GenericStack <double>(size); GenericStack <string> stack2 = new GenericStack <string>(size); // Push elements on the stack try { for (int i = 0; i <= size; i++) { stack.Push(i); Console.WriteLine("Push: {0}", i); } } catch (ApplicationException ex) { Console.WriteLine("Error while pushing values on the stack: {0}", ex.Message); } // Pop elements from the stack double total = 0.0; try { for (int i = 0; i <= size + 5; i++) { // Note, no casting needed. double value = stack.Pop(); total += value; Console.WriteLine("Pop: {0}", value); } } catch (ApplicationException ex) { Console.WriteLine("Error while poping values from the stack: {0}", ex.Message); } Console.WriteLine("Total: {0}", total); // Using Generic methods int sz1 = 10; int sz2 = 6; GenericStack <double> stackA = new GenericStack <double>(sz1); GenericStack <double> stackB = new GenericStack <double>(sz2); GenericMethod.Swap <GenericStack <double> >(ref stackA, ref stackB); Console.WriteLine("Sizes of stacks: {0} {1}", stackA.Size(), stackB.Size()); // Swap 2 doubles double d1 = 1.2; double d2 = 3.0; GenericMethod.Swap <double>(ref d1, ref d2); Console.WriteLine("Sizes of stacks: {0} {1}", d1, d2); }
public static void Main() { // Create object with the generic swap method GenericMethod gm = new GenericMethod(); // Value type double d1 = 2.79; double d2 = 3.14; // Struct (value type) Point p1 = new Point(1, 2); Point p2 = new Point(3, 4); // Class (reference type) Exception e1 = new Exception("Exception 1"); Exception e2 = new Exception("Exception 2"); // Print values Console.WriteLine("Original values"); Console.WriteLine("Doubles: {0}, {1}", d1, d2); Console.WriteLine("Points: {0}, {1}", p1, p2); Console.WriteLine("Exceptions: {0}, {1}", e1, e2); // Swap the values gm.Swap <double>(ref d1, ref d2); gm.Swap <Point>(ref p1, ref p2); gm.Swap <Exception>(ref e1, ref e2); // Print values again Console.WriteLine("\nSwapped values"); Console.WriteLine("Doubles: {0}, {1}", d1, d2); Console.WriteLine("Points: {0}, {1}", p1, p2); Console.WriteLine("Exceptions: {0}, {1}", e1, e2); // Swap the values gm.Swap(ref d1, ref d2); gm.Swap(ref p1, ref p2); gm.Swap(ref e1, ref e2); // Print values again Console.WriteLine("\nSwapped again values"); Console.WriteLine("Doubles: {0}, {1}", d1, d2); Console.WriteLine("Points: {0}, {1}", p1, p2); Console.WriteLine("Exceptions: {0}, {1}", e1, e2); // Call with specifying the type Console.WriteLine("\n\nCall generic print with type"); gm.Print <double>(3.14); gm.Print <Exception>(new Exception("Exception")); // Call without specifying the type Console.WriteLine("\nCall generic print without type"); gm.Print(3.14); gm.Print(new Exception("Exception")); }