Getting Started with Adobe After Effects - Part 6: Motion Blur


Upload Image Close it
Select File

Browse by Tags · View All
BRH 48
#DOTNET 34
#ASP.NET 29
jQuery 22
ASP.NET 20
.NET 20
WPF 9
jquery interview questions 9
jquery faq 8
ASP.NET4 8

Archive · View All
February 2011 10
September 2011 4
August 2011 4
July 2011 4
May 2011 4
April 2011 4
March 2011 4
October 2011 4
June 2011 4
January 2011 4

All about Generics in C#

Jan 25 2011 11:05PM by Hima   

 Introduction

Generics in C# are an extension to C# Programming using Generic Types and Methods. I see most of the developers still get confused about generics and do not know to use them in real time projects. That made me to write this article to help understand and refer how to use them?

Basic concept is similar to Templates in C++ with better extensibility and usability. The C++ Templates are compile time Feature whereas Generics is a feature of runtime.

These are a type of data structure that contains code, remains the same. The data type of the parameters will change for each use. We can also call them parameterized types or parametric polymorphism. Generics allow classes, structs, interfaces, delegates, and methods to be parameterized by the types of data they store.

The usage within the data structure adapts to the different data type of the passed variables. This Allows defining type-safe data structures need less committing to actual data types. We reuse data processing algorithms without duplicating type-specific code.

System.Collections.Generic

The System.Collections.Generic namespace contains several generic collection classes based on generics and it is recommended that we should use these collection classes in place of the earlier non-generic ones for better performance of our applications.

The namespace  contains a lot of generic classes, structures, and interfaces like

  • Dictionary
  • List
  • Queue
  • SortedDictionary
  • Stack

Examples

public class Gen<T>
        {
           T t; 
           public T Val
           {
               get;
               set;

           }
       }

The class name " Gen<T” represents that the Type is generic, specifically the brackets <>containing the Type. This Type "T" is used to show that if we need to refer to the actual Type that is going to be used when we write this class".

T t; Creates member of the type T

The generic Type that we will specify during construction of the class will get inserted by the Common Language Runtime (CLR). The final item in the class is the public property. Again, notice that we are using the Type placeholder "T" to represent that generic type for the type of that property. Also notice that we can freely use the private variable "t" within the class.

In order to use this class to hold any Type, we simply need to create a new instance of our new Type, providing the name of the Type within the "<>" brackets and then use that class in a Type-safe manner as shown below.

static void Main(string[] args)
        {
            //create a string version of our generic class
            Gen str = new Gen();
            //set the value for the string type
            str.Val = "Beyond Relational";

            //output the type and value
            System.Console.WriteLine(str.Val);
            System.Console.WriteLine(str.Val.GetType());

            //create another instance of our generic class, using a different type
            Gen flt = new Gen();
            flt.Val = 12.34;
            System.Console.WriteLine(flt.Val);
            //output 
            System.Console.WriteLine(flt.Val.GetType());
            Console.ReadLine();

        }  

}

Generic Collections

public class MessageElement
{
 //some functionality 
}
public class MessageSearchCriteria
{
// some functioanlity
}

public List ListMessageElement
    {
        get       
        {
            return lstMsgElemet;
        }
        set
        {
            lstMsgElemet = value;
        }
    }

MessageSearchCriteria objMsgSearch = new MessageSearchCriteria();
//usage of generics
objMsgSearch.ListMessageElement = new List();

Advantages

  • Help to make the code in the software components much more reusable.
  • High Quality Code and Code Maintainability
  • No runtime Casting No Boxing hence Allows Performance Efficiency
  • Creates Type safe at compile time as generics specify type at runtime
  • Programmer has stronger and flexible control over the code
  • The stronger type checking associated with generics helps in finding errors easily at compile time only.
  • No code Duplication

Generics refer to classes and methods that work consistently on values of different types.

Best Practices to use

  1. If the data type contains Collection Type or any unspecified data types, then create a generic type.
  2. If the data type will be operating on value types, then using generics will prevent the boxing and un boxing operations.
  3. If the code contains multiple classes to handle different data types on which they operate then it is best practice to use Generics.

Tags: BRH, C#.NET, Generics, C# 2.0, Cs.NET,


Hima
31 · 6% · 1776
2
 
0
Lifesaver
 
0
Refreshed
 
 
0
Incorrect



Submit

1  Comments  

  • Hi, In the second example you are instantiating the generaic class like Gen str = new Gen();

    i thing we should use angle braces for int and string type

    Thanks Jayant

    commented on Jun 27 2011 12:23AM
    Jayant
    3057 · 0% · 2

Your Comment


Sign Up or Login to post a comment.

"All about Generics in C#" rated 5 out of 5 by 2 readers
All about Generics in C# , 5.0 out of 5 based on 2 ratings
    Copyright © Rivera Informatic Private Ltd Contact us      Privacy Policy      Terms of use      Report Abuse      Advertising      [ZULU1097]