Arrao4u

…a blog by Rama Rao

Archive for the ‘Generics Part1’ Category

Generics part1

Posted by arrao4u on December 13, 2009

Generics Part1

1 // Fig. 25.3: GenericMethod.cs
2 // Using overloaded methods to print arrays of different types.
3 using System;
4 using System.Collections.Generic;
5
6 class GenericMethod
7 {
8 static void Main( string[] args )
9 {
10 // create arrays of int, double and char
11 int[] intArray = { 1, 2, 3, 4, 5, 6 };
12 double[] doubleArray = { 1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7 };
13 char[] charArray = { ‘H’, ‘E’, ‘L’, ‘L’, ‘O’ };
14
15 Console.WriteLine( “Array intArray contains:” );
16 PrintArray( intArray ); // pass an int array argument
17 Console.WriteLine( “Array doubleArray contains:” );
18 PrintArray( doubleArray ); // pass a double array argument
19 Console.WriteLine( “Array charArray contains:” );
20 PrintArray( charArray ); // pass a char array argument

PrintArray< int >( intArray ); // pass an int array argument
21 } // end Main
22
23 // output array of all types
24 static void PrintArray< E >( E[] inputArray )
25 {
26 foreach ( E element in inputArray )
27 Console.Write( element + ” ” );
28
29 Console.WriteLine( “\n” );
30 } // end method PrintArray
31 } // end class GenericMethod

Array intArray contains:
1 2 3 4 5 6

Array doubleArray contains:
1.1 2.2 3.3 4.4 5.5 6.6 7.7

Array charArray contains:
H E L L O

http://www.deitel.com/articles/csharp_tutorials/20051111/GenericMethodImplementation.html

Posted in C#, Collections, Generics, Generics Part1 | Leave a Comment »