Using Generics in C#
page 2 of 7
by Joydip Kanjilal
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 37755/ 56

What are Generics?

According to MSDN, "Generics introduce to the .NET Framework the concept of type parameters, which make it possible to design classes and methods that defer the specification of one or more types until the class or method is declared and instantiated by client code." By using Generics, classes and methods can work uniformly on the values of different types. Generics facilitate type safety, improved performance and reduced code. It promotes the usage of parameterized types on our types and is also known as parametric polymorphism. The Common Language Runtime (CLR) compiles any Generic type to IL and Metadata as it does with the other types; but it stores added information pertaining to the generic types which is used to bind the generic type to a specific type at runtime when the generic type is instantiated. Note that for generic types that are bound to value types, the generic types are instantiated for each value type that it is bound to. Unlike this, for generic types that are bond to reference types, the generic type instance refers to the location in memory of the reference type to which it is bound for all the instances of the generic type. The following code snippet in Listing 1 illustrates how a generic type can be implemented.

Listing 1

public class Test<T> 
{
    public void Display() 
      {
          //Some code
      }
//Other members
}

We may say that the type parameter is an unbound type as it is not bound to any specific type. When we instantiate this class, the generic type has to be bound to a particular type as shown in the code snippet in Listing 2.

Listing 2

Test<int> test = new Test<int>();

Note that in the code snippet above, the resulting type called "test" is a bound type. The code example in Listing 3 shows how we can use a generic method on varied types.

Listing 3: Implementing a generic method

using System;  
using System.Collections.Generic;  
 
   class Test  
   {  
      static void Main( string[] args )  
      {  
        Test t = new Test();
        int[] integerArray = {1,2,3,4,5,6};  
        char[] characterArray = { 'J', 'O', 'Y', 'D', 'I','P' };   
        double[] doubleArray = {0.1,0.2,0.3,0.4,0.5,0.6};  
         Console.WriteLine( "Displaying the contents of the integer array:--" );  
         t.Display(integerArray);
         Console.WriteLine( "Displaying the contents of the character array:--" );
         t.Display(characterArray);
         Console.WriteLine( "Displaying the contents of the double array:--" );
         t.Display(doubleArray);
      }   
     
      public void Display< GenericArray >( GenericArray[] array )  
      {  
         for (int i = 0; i< array.Length; i++)  
            Console.WriteLine(array[i]);  
      }
   }

The following points sum up the basic advantages of using Generics.

·         Code Efficiency

·         Enhanced performance

·         Type Safety and reliability

·         Maintainability of code


View Entire Article

User Comments

No comments posted yet.






Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2024 ASPAlliance.com  |  Page Processed at 2024-04-24 2:50:12 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search