-----------------------------------------------------------

New Message on BDOTNET

-----------------------------------------------------------
From: Anand_2004
Message 1 in Discussion

  
Hi Folks, 
Today I am covering Generics .This is one of the great feature of  C# 2.0 
infect I like it very much. Generics  has lots of cool stuffs .I am going to 
write  a series of  articles in order to cover it in-detail. This is first part 
of  my article on Generics.Generics is a mechanism that encapsulate operations 
that are not specific to any particular data type, that means it  allows  
classes or  structs or  interfaces or  methods to be parameterized by the types 
of data they store and manipulate. Generics provides the benefits like 
reusability, type safety and efficiency in a way that their non-generic 
counterparts cannot. Generics are most commonly used with collections and the 
methods that operate on them. The .NET framework 2.0 provides a new namespace 
System.Collections. Generics which contains several new generics based 
collection classes apart from this you can also create custom generic types and 
methods to provide your own generalized solutions that are type-safe and 
efficient.  
If we look at the collection classes of  .NET Framework 1.X there are certain 
limitations like it needs to perform boxing/unboxing  or 
up-casting/down-casting  while storing and retrieving of values for example if 
we store value type in ArrayList it performs boxing/unboxing  or if  store 
string in ArrayList it performs up-casting and down-casting because it takes 
System.Object as parameter , the other limitation is lack of compile-time type 
checking; since an ArrayList casts everything to Object  to over come the above 
problems  in .NET framework 1.X   we can write our own type specific collection 
class ( custom collection class) but again such class are not generic ( 
reusable) for more than one data type and you have to rewrite different custom 
class for each data types. 
The ultimate solutions to this problems is ArrayList needs a type parameter. 
That is precisely what generics provide. In the generic List<T> collection, in 
the System.Collections.Generic namespace, the same operation of adding items to 
the collection looks like this 
List<int> mylist = new List<int>();<O:P> </O:P> 
//No boxing, no casting:<O:P> </O:P> 
mylist.Add(99); <O:P></O:P> 
//Compile-time error:<O:P> </O:P>mylist.Add("Wrong type passed");   
Similarly you can define your custom generic class for example 
public class MyList <T> 
{ 
T[] items; 
int counter; 
public void  Add ( T item) 
{ 
// code here 
} 
public void  Remove (T item) 
{ 
// code here 
} 
} 
in the above example generics  provide a facility for creating types that have 
type parameters ( MyList class with type parameter T). While instantiating 
MyList<T> specify  the type  for which they are created and store data of that 
type without conversion to and from System.Object. Actually the type parameter 
<T> acts as a placeholder until an actual type is specified at use  as below 
MyList<int> objList = new MyList<int>();
objList.Add(3); // this eliminate boxing/unboxing or casting operation and 
provide strong type 
its a compile time error is we add text to this list fro example 
objList.Add("This is a test message");//compile-time error 
The above example demonstrate only one type , Generic type declarations may 
have any number of type parameters lets say for hashtable we need key and value 
for this you need to specify the number of parameters while define the class as 
below 
public class MyHash<T Key,T Value> 
{ 
//reset of the code here 
} 
Cheers 
Anand 
http://spaces.msn.com/members/anandkumar

-----------------------------------------------------------

To stop getting this e-mail, or change how often it arrives, go to your E-mail 
Settings.
http://groups.msn.com/bdotnet/_emailsettings.msnw

Need help? If you've forgotten your password, please go to Passport Member 
Services.
http://groups.msn.com/_passportredir.msnw?ppmprop=help

For other questions or feedback, go to our Contact Us page.
http://groups.msn.com/contact

If you do not want to receive future e-mail from this MSN group, or if you 
received this message by mistake, please click the "Remove" link below. On the 
pre-addressed e-mail message that opens, simply click "Send". Your e-mail 
address will be deleted from this group's mailing list.
mailto:[EMAIL PROTECTED]

Reply via email to