Prizes & Awards
My Profile
Active Members
TodayLast 7 Days
more...
|
New Feature: Community Sites:
Create your own .NET community website and start earning from Google AdSense !
It's Free !
|
Generics In C#
|
We have worked with Collections in Visual Studio 2003 and seen the power of it, but collections deals with all types of data. What I mean is that in a collection we can add any object regardless of the type. Often we face situations where we want to create a collection of a specific type i.e. it expects objects of specific types only.
Visual Studio 2005 has given us this amazing feature which can be used with collections that makes your code more usable and saves you from explicit type conversions. The generics come under the namespace System.Collections.Genric, The Microsoft team also recommends to users using .Net Framework to use generics instead of the traditional ArrayList
With generics, however, wean avoid the messy and intensive conversions from reference types to native types. Additionally, we can create routines that are much more type-safe. It is more close to the concept of templates in C++ with some additional features added to it. Generics can be used with any data type ranging from integer to complex data types such as structures. An example of sing generics in C# is given below:
Compare compare = new Compare; int getInt = compare.Larger(3, 5);
Generics also support some additional refinements. Example, a method/class may have one or more than one parameterized type, and generics in C# also supports WHERE constraints that refine the type of the parameterized types.
Parameterization is used not only for classes but also for interfaces, structs, methods and delegates.
//For Interfaces interface IDeletable
//for structs struct SYSDATETIME
//for methods static void ToDateTime? (T[] arr)
//for delegates delegate void TakeAction? (T arg)
Following are some of the core benefits we get from Generics in C#, and which make it so special.
Efficiency: performance is boosted by some of the below mentioned points.
1. Instantiations of parameterized classes are dynamically loaded and the code for their methods is generated on demand.
2. Compiled code and data representations are shared between different instantiations.
3. Due to type specialization, the implementation never needs to box values of specific types.
Safety: Strong type checking at compile time, hence more bugs caught at compile time itself.
Maintainability: Maintainability is achieved with fewer explicit conversions between data types and code with generics improves clarity and expressively.
Generics allow a developer to author, test and deploy the code once and then enjoy the fruit of reusing the same code again for a variety of data types. More than that Generics are checked at Compile-Time. When the program instantiates a generic classusing the supplied parameter, the parameter can only be of the type your program specified in the class definition.
|
Responses
|
No responses found. Be the first to respond and make money from revenue sharing program.
|
|