difference between generic class and non generic class ?

Posted by Bageshkumarbagi under C# on 11/16/2012 | Points: 10 | Views : 31949 | Status : [Member] | Replies : 2
hi

difference between generic class and non generic class. plz give some example also.

Thanks,
Bagesh Kumar Singh
Mark as answer if you have been satisfied



Responses

Posted by: Sandhyab on: 11/16/2012 [Member] Starter | Points: 25

Up
1
Down
Hi,
Generic collections - These are the collections that can hold data of same type and we can decide while initializing what type of data that collections can hold.
Advantages - Type Safe, Secure, reduced overhead of implicit and explicit conversions.
Non generic collections hold elements of different datatypes, it hold all elements as object type.
so it includes overhead of implicit and explicit conversions.

Bageshkumarbagi, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Nkkppp on: 11/16/2012 [Member] Starter | Points: 25

Up
0
Down
Generic class is a class whose objects each can operate on different type of data.

Below is the syntax to create generic classes

[accessmodifier] class classname<generictypes>
{
members..fields
}

Note: While creating an object for the same do remember that we need to specify the datatypes on which that object has to operate ie.

Classname<datatypes> objname=new Classname<datatypes>();

Below is an example


class GenericClasses

{
class Test<Mytype>
{
Mytype A,B;
public Test(Mytype x,Mytype y)
{
A=x;
B=y;
}
public void Print()
{
Console.Writeline("A:{0}\t" B:{1}",A,B);
}
}
Static Void Main()
{
Test<int>T1=new Test<int>(10,1)();
Test<double> T2=new Test<double>(1.25,2.25)();
Test<string> T3=new Test<string>("XYZ","ABC")();
T1.Print();
T2.Print();
T3.Print();

}
}
}



So this class can operate on any kinda object.But in the non-generic class you need to create a separate constructor for double and string as per above example.

Hope this helps you in understanding the differences.

---Prathap.

Bageshkumarbagi, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response