Hi
To begin with “Wish you all a Happy New Year 2009 !!!”
I am starting this Blog on this New Year on Jan’01 2009
In this blog i am planning to elaborate the various advanced features available in .Net across various version, I plan to use c# as the programming language to explain the new features. Welcome you all to join this journey with me.
The .Net framework has evolved over a period of time, starting with Framework 1.0 version there were a series of version upgrades like 1.1, 2.0, 3.0 & 3.5. Every version upgrade brings with it a range of new features to enhance development and to make life easier. Also every new version brings new features to assist the changing business needs of the world.
It is evident that the features added are to satisfy the changing business needs, for example we see that more and more support is being added to XML and Web based development in each new version on .Net, this is because XML has become an easy and reliable way of sharing data between various heterogeneous systems. Obviously the Web is taking new dimensions with every passing day, eCommerce has brought in revolution in the retail world by enabling end users to purchase any thinks in the world with the click of a button. Due to these changing needs, Microsoft is adding more and more support to Web development and XML in every passing version of the .Net Framework.
To begin with we shall start with a new concept called “Generics” added to .Net Framework 2.0
Generics
In .Net individual objects are type safe, meaning that we cannot just cast an object and assign it to another of a different type, the compiler performs type-safety checks while assigning values to variables.
clsCars objCars = new clsCars();
clsTrucks objTrucks = new clsTrucks();
The following assignment throws an error
objCars = (clsCars) objTrucks;
This type safe checking is restricted to individual objects only; collections do not have this restriction, hence collections can hold any type of objects, for example
ArrayList objVehicle = new ArrayList();
objVehicle.Add(objCars);
objVehicle.Add(objTrucks);
Now we do not have any control over the type of object which is stored in the collection, and there is no way in which we can cast and re-use each of the objects stored in the collection.
This limitation is overcome in .Net 2.0 with the help of Generics, Generics allow collections to be type safe at compile time. The System.Collections.Generics
namespace enables use of Generics in .Net 2.0. The System.Collections.Generic
, contains the classes that support this concept, like the List
, Queue
, Stack
, LinkedList etc.
Generics are similar to 'Templates' in C++, which makes it possible to design classes which take in a generic type and determine the actual type later on.
As the name suggests Genreics are neutral collection, not bound to any type initially, once a generic is declared we can use it for any datatype, but we can use it to store items of only one specific datatype at any given point of time, thereby making them type safe.
Syntax for declaring Generics
clsMyGereric
Here T is the datatype which we want the Generic to hold and this can be determined at compile time.
Example
Declaring the Generic Class:
public class clsMyGeneric
{
T t;
public T Val{get{return t;}set{t=value;}}
}
Here ‘T’ is a generic datatype, we can replace ‘T’ with any datatype at compile time.
Using the Generic class:
clsMyGeneric
//
objName,Val = “Name1”
//
clsMyGeneric
//
objAge.Val = 35;
Generic Collections
Now that we have defined individual Generics, we need a Generic collection to store these objects, let us see how to define Generic collections.
System.Collections.Generic.List objNameList
= new System.Collections.Generic.List();
Now we can add any variable of type string to the above defined collection, but remember that we can add only objects of string datatype to the collection, if we try to add objects of a different type then an error will be throws, thereby we have achieved type safe collections in .Net 2.0
objNameList.Add(objName.Val) -- Correct
objNameList.Add(“Name2”) -- Correct
objNameList.Add(objAge.Val) -- Throws a exception, since the value is of type int.
To Summarize, a Generic type/class is one which by declaration is generic/neutral by datatype and can hold any datatype, but only one data type based on how it is initialized, similarly a Generic collection in one which can hold objects of only one datatype and this type can be determined at compile time.
We have come to the end of my first blog post, hope this information was useful to you, keep watching this post for more updates soon ...
No comments:
Post a Comment