Generics
-
Hi Friends I would i like question u all abt Generics. I have read abt Generics but there is a doubt & i would i like to question u using simple program. public class Student<T> { private string name = "satish"; private Int16 RollNo = 12; public void displayStudent() { Console.WriteLine(name); Console.WriteLine(RollNo); Console.ReadLine(); } } class Program { static void Main(string[] args) { Student<int> s1 = new Student<int>(); s1.displayStudent(); } } If i am right then the above Student class is called Generic class. What is the use of creating a class like this if we are using a method & we are just specifying the Type in angle bracket. so Please help me :)
-
Hi Friends I would i like question u all abt Generics. I have read abt Generics but there is a doubt & i would i like to question u using simple program. public class Student<T> { private string name = "satish"; private Int16 RollNo = 12; public void displayStudent() { Console.WriteLine(name); Console.WriteLine(RollNo); Console.ReadLine(); } } class Program { static void Main(string[] args) { Student<int> s1 = new Student<int>(); s1.displayStudent(); } } If i am right then the above Student class is called Generic class. What is the use of creating a class like this if we are using a method & we are just specifying the Type in angle bracket. so Please help me :)
Hy, This MSDN usage definition on Generic Types is the best I think:
- You can use generic types/classes to maximize code reuse, type safety, and performance.
- The most common use of generics is to create collection classes.
List<T> is possibly one of the best example: It has generic type in that you can create a List of ints, strings... your own defined types. It has type safety => you can not add "Dan" to a list if ints It has performance => no need to cast from object to yourType [EDIT]Here's a better usage of generics for your example:
public class Student
{
private string name = "satish";
private Int16 RollNo = 12;public void displayStudent()
{
Console.WriteLine(name);
Console.WriteLine(RollNo);
Console.ReadLine();
}
}class Program
{
static void Main(string[] args)
{
List<Student> students = new List<Student>();
students.Add(new Student());
foreach(var std in students){
std.displayStudents();
}
}
}[/EDIT]
modified on Monday, February 22, 2010 1:12 AM
-
Hy, This MSDN usage definition on Generic Types is the best I think:
- You can use generic types/classes to maximize code reuse, type safety, and performance.
- The most common use of generics is to create collection classes.
List<T> is possibly one of the best example: It has generic type in that you can create a List of ints, strings... your own defined types. It has type safety => you can not add "Dan" to a list if ints It has performance => no need to cast from object to yourType [EDIT]Here's a better usage of generics for your example:
public class Student
{
private string name = "satish";
private Int16 RollNo = 12;public void displayStudent()
{
Console.WriteLine(name);
Console.WriteLine(RollNo);
Console.ReadLine();
}
}class Program
{
static void Main(string[] args)
{
List<Student> students = new List<Student>();
students.Add(new Student());
foreach(var std in students){
std.displayStudents();
}
}
}[/EDIT]
modified on Monday, February 22, 2010 1:12 AM
hi sir, Thank u that u replied soon sir But what happens in Memory. List is of Object type & if i specify Int is of Value type. Then what exactly happens ? Thank u once again
-
hi sir, Thank u that u replied soon sir But what happens in Memory. List is of Object type & if i specify Int is of Value type. Then what exactly happens ? Thank u once again
-
ya i saw your Program sir. now i understood thank u very much :)