Click here to Skip to main content
13,344,508 members (48,786 online)
Click here to Skip to main content
Add your own
alternative version


12 bookmarked
Posted 2 Sep 2002

Understanding the Generic Type in C#

Rate this:
Please Sign up or sign in to vote.
Storing value and reference types in the generic type in C#


Before we start tampering around the code, let's understand why we need a generic type. In most of application development we come across situations where we need a type that can hold both value types and reference types. In this situation we need a generic type which can do both. Before we start working on a generic type we'll talk a little about types in .NET.

As we know there are two fundamental categories of types

  1. value types (example: structures, primitive type, enums)
  2. reference types (example: Classes, interface, arrays, strings etc..,)

So when you are using any one of the above types they are bonded to that particular type. For example if we declare a structure it bonds to a value type and when we declare a class it bonds to a reference type. Despite the fact that the declaration syntax is same for both but the semantics are distinct. And in terms of memory allocation for a value it is on the stack, whereas for reference type it is on the managed run-time heap.

Assignment to a variable of a value type creates a copy of the value.

For example:

using System;
public class valTyp
public static void Main()
Int32 I=10,J=0;
// this will create a copy value I into J 
Console.WriteLine( "I value: {0}, J value {1}",I,J);


I value: 11, J value 10

The assignment to the local variable J does not impact the local variable I because both local variables are of a value type (the type Integer) and each local variable of a value type has its own storage.

But assignment to a variable of a reference type creates a copy of the reference, not a copy of the value being referenced. Let’s see this with an example below.

For example:

using System;
public class Cclass
	public int val = 0;
public class refTyp
	public static void Main()
Cclass ref1 = new Cclass();
Cclass ref2=ref1;
ref2.val = 555;
Console.WriteLine( "Before increament Ref1 value: {0}, Ref2 value {1}",ref1.val,ref2.val);
Console.WriteLine( "After increament Ref1 value: {0}, Ref2 value {1}",ref1.val,ref2.val);


Before increament Ref1 value: 555, Ref2 value 555
After increament Ref1 value: 556, Ref2 value 556

In contrast with value type the assignment to reference type variable ref2.val = 555, affects the object that both ref1 and ref2 reference.

From above we see that value types and reference types are different. So to create a Generic type we need something which is neither of them. In .NET we have the root type Object (alias System.Object) which is special in that it is neither a reference type nor a value type, and may not be instantiated. So a variable of type Object can either contain a value type or a reference type. The question comes to mind: Why can the Object class can handle both?

The answer is both reference and value types are derived from the base class Object. So using the concept of boxing and unboxing we can treat any type as an object. So what does boxing do, and where is it necessary for a value type to behave like an object? Boxing uses wrappers which make the value type look like a reference object (allocated on heap), and the value type’s value is copied into it. Then this complete wrapper is marked so that the CLR knows that it contains a value type. Similarly the reverse process is called as unboxing.

Let’s see a sample code using Object class as Generic type:

// objapp.cs 
using System;

public class Myclass
  public string name="Hello from Myclass";
  public void doSomething()

public class ObjApp{
 public static void CheckType(object o)
     if (o is string)
         Console.WriteLine ("o is String");
         string s = (string)o;
         Console.WriteLine (s);
      else if (o is Int32[])
         Console.WriteLine ("o is Int32 array");
         Int32[] ins=(Int32[])o;
         for(Int32 i=0;i< ins.Length;i++)
         Console.WriteLine (ins[i]);
      else if (o is Myclass)
         Console.WriteLine ("o is Myclass");
           Myclass a = (Myclass)o;

   public static void Main(){
      // Create an array of Object references
      Object[] objs = new Object[3];   
      objs[0] = "Some String";        // create a new string
      objs[1] = new Int32[]{1, 2, 4}; // create a new Int32 array
      objs[2] = new Myclass();         // create a new Object instance
      for(Int32 ind = 0; ind < 3; ind++)
         Console.WriteLine("Array index {0} refers to {1}",ind, objs[ind]);


Array index 0 refers to Some String
o is String
Some String
Array index 1 refers to System.Int32[]
o is Int32 array
Array index 2 refers to Myclass
o is Myclass
Hello from Myclass

In the above program if you see an Object class it can take any type and in the CheckType member function we are sending the generic type (Object). Using the is operator we are checking the type which is passed as a parameter to the function. Then using Boxing we can point to the specific type and use it for further processing.

Further reading

  1. .NETMore information on .NET technologies


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Chandra Hundigam Venkat
United States United States
Chandra Hundigam has Master degree in Computer Application, Microsoft Certified Professional and Software Architect. He's significantly involved in enterprise application development and distributed object oriented system development using Microsoft .Net, Sun Java/J2EE technology to serve global giants in the Media, Finance, Mortgage and Software Industries.Presently working as Independent Software Consultant for a US-based company.His areas of interests are in emerging Technologies.

You may also be interested in...


Comments and Discussions

AnswerGenerics Pin
Shail Mishra21-Mar-14 23:37
memberShail Mishra21-Mar-14 23:37 
GeneralMy vote of 1 Pin
Member 100361609-Oct-13 2:34
memberMember 100361609-Oct-13 2:34 
QuestionGeneric Types? Pin
Horia Tudosie4-Jul-07 11:59
memberHoria Tudosie4-Jul-07 11:59 
GeneralCopy an object by value Pin
javier_glez_carpio10-Jul-03 1:08
memberjavier_glez_carpio10-Jul-03 1:08 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.180111.1 | Last Updated 3 Sep 2002
Article Copyright 2002 by Chandra Hundigam Venkat
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid