Emailid
Password
         
  
    Forgot password

New user Sign Up
 

Generics

       Current Rating:  0%                                                     Total Members Rated:  0
                                                                     Send To Friend

 

Generics

 

 

Introduction

 

Programmers need more and more a way to better reuse and customize their existing component-based software. C# will include a type-safe, high-performance version of generics which differs slightly in syntax and greatly in implementation from templates as found in C++ and generics as proposed for the Java language. To achieve such a high level of code reuse in other languages, programmers typically employ a feature named generics.

 

 

Constructing Generic Classes

 

Programmers can create a limited version of true generic types by storing data in instances of the base object type in C#. Since every object in C# inherits from the base object type, and because of the boxing and unboxing features of the unified .NET type system, programmers can store both reference and value types into a variable whose type is object. There are performance penalties for converting between reference types and value types and the base object type in C#.

 

 

The following code sample generates a simple Stack type with two actions, "Push" and "Pop". The Stack class stores its data in an array of object types, and the Push and Pop methods use the base object type to accept and return data, correspondingly:

 

public class Stack { private object[] items = new object[100]; public void Push(object data) { ... } public object Pop() { ... } }

 

Here, a custom type can be pushed onto the stack. However, if your program needs to retrieve the data, it needs to explicitly cast the result of the Pop method, a base object type, into a Customer type.

 

Stack s = new Stack(); s.Push(new Customer()); Customer c = (Customer) s.Pop();

 

When a value type is passed to the Push method, the run-time automatically converts it into a reference type - a process known as boxing - and then stores it in the internal data structure. Similarly, if you want your program to retrieve a value type, such as an integer, from the stack, your program needs to explicitly cast the object type obtained from the Pop method into a value type, a process known as unboxing:

 

Stack s = new Stack(); s.Push(3); int i = (int) s.Pop();

 

The boxing and unboxing procedures between value and reference types can be particularly onerous.

 

 It is not possible to enforce the kind of data placed in the stack in the current implementation. Of course, a stack could be created and then a Customer type could be pushed onto it. Later, the same stack could be used and attempt to pop data off it and cast it into a different type.

 

Example:

Stack s = new Stack(); s.Push(new Customer()); Employee e = (Employee) s.Pop();

 

While the previous code sample is an improper use of the single type Stack class that is wanted for implementation and should be an error, it is actually legal code and the compiler would not have a problem with it. At run-time, however, the program would fail due to an invalid cast action.

 

All the best!


                           Rate This Article:   

Author is Offline
  Author: Helen Deol
       


Comments Posted
Label
Subject Author Status Date

 

Post Comment

Related Articles
Overwhelming and Creation of Generics in C#
Benefits of Generics
C## Generics in the Runtime
Dissimilarities between Other Implementations and C# Generics
C# Generics



Home | About Us | Site Map | Privacy Policy | Submit Links