Posts

Showing posts with the label Generic

Generic Methods

Generics in C# Generic in C# means common to or applicable to an entire class. As most of the developers think Generic is to define type-safe data structures without committing to actual data types, but is it the only reason why Generics are for? The answer is BIG NO . In this article we will focus on what other important aspect can be achieved in our daily programming by using Generic. We will see how to use Generic and avoid method overloading. The below program shows the use of method overloading to display content of int, double & char array. using System; class OverloadedMethods { static void Main(string[] args) { // create arrays of int, double and char int[] intArray = { 1, 2, 3, 4, 5, 6 }; double[] doubleArray = { 1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7 }; char[] charArray = { 'H', 'E', 'L', 'L', 'O' }; Console.WriteLine("Array intArray contains:"); DisplayArray(intArray); // pass an int array argumen