Home > .Net Fundations, Computer Science > .Net Generics (Performance and flexibility)

.Net Generics (Performance and flexibility)

I discussed the reason of using Generics in another article. Here it’s aimed to talk about Generics in detail.

Generics are a part of CLR type system that allows you to develop your own types which are flexible in their parameters’ type. Therefore, it allows you to help your consumers feel comfortable while using your class. The most common use for Generics is data types such as lists, linked lists, stacks, trees etc where the operations on each item is done in the same way regardless of their types.

MS Generics

Fig 1

Why using Generics:

1-      Improve performance: As it’s discussed in my previous article, assume that you have a class that its parameters’ type is not specified. You have two choices to build it. One way is to use Object as your parameters’ type which in most cases it needs boxing and unboxing. The second way is to use Generics which has no boxing and unboxing but has almost the same flexibility as Object hast.

2-      Less runtime errors: While you are using Object you need to convert it in order to use it. And because it is not possible to check the parameters’ type in compile-time compiler needs to do type-checking in runtime. In this case, if you cast it to wrong type compiler raises type mismatch error. In contrary, using Generics doesn’t have this issue since its strongly-typed data type, compiler can catch this matching at compile-time.

 

Creating generics types:

It is quite a fun. You just need to specify the name of parameters that are type flexible. After that everything is done by .Net.

class GenericsTemplate<T, U>

{

T t;

U u;

public GenericsTemplate(T t, U u)

{

this.u = u;

this.t = t;

}

public string getT

{

get { return t.ToString(); }

}

public string getU

{

get { return u.ToString(); }

}

}

Here you have a flexible class which can accept all possible types! Isn’t it easy!! :D. It can be int, string, a custom class or any other type.

How to use it:

Parameters’ type are defined in class instantiation.

class Program

{

static void Main(string[] args)

{

GenericsTemplate<string, int> genClass = new GenericsTemplate<string, int>("Hello World", 560);

Console.Write(genClass.getT + Environment.NewLine + genClass.getU);

Console.ReadKey();

}

}

Since parameters’ type is specified in declaration, compiler can check for any mismatch error.

How to use constraints:

Did you find it useful? But I think you found some limitations in using these variables inside the generic class. You can only have capabilities of base Object data-type which are ToString() and GetHashCode() methods. To overcome this problem .Net introduces a key word called where. This key word can be used to limit the range of possible types that parameter’s can accept. This limitation may also reduce the complexity of consuming your generic class since developers (consumers) won’t be confused among too many possible types can be used.

This limitation can be applied for:

1-      Reference or value type: Forces a parameter to accept data-types which are either reference type or value type. (Key words are class for reference type and struct for value type)

2-      Interface: Parameter can accept types which have a specific implemented interface.

3-      Base class: Parameter can accept types which have a specific inherited interface.

4-      Constructor: Forces a parameter to accept data-types which has a default constructor (parameterless constructor). (Key word is new())

class Program

{

static void Main(string[] args)

{

System.Drawing.Bitmap bt = new System.Drawing.Bitmap(800, 500);

//Bitmap is an inherited class from Image and int is a value type which has implemented IComparable interface

GenericsTemplate<System.Drawing.Bitmap, int> genClass = new GenericsTemplate<System.Drawing.Bitmap, int>(bt, 560);

Console.Write(genClass.getT + Environment.NewLine + genClass.getU);

Console.ReadKey();

}

}

//T should be a reference type and an instance of Image class or any class inherited from Image. U sould be value type and has implemented IComparable interface

class GenericsTemplate<T, U> where T: class, System.Drawing.Image where U: struct, IComparable

{

T t;

U u;

public GenericsTemplate(T t, U u)

{

this.u = u;

this.t = t;

}

public string getT

{

//You can use all properties of Image base class while useing t

get { return t.Width.ToString(); }

}

public string getU

{

get { return u.ToString(); }

}

}

You should follow a correct order (order as listed above).

After defining constraints for generic class you can use added capabilities of parameters inside the class. Here I used Width property of base Image class.

Advertisements
  1. Geek
    April 28, 2011 at 3:16 am

    Short and useful, thanks

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: