| 1. |
What Is A Generic Class? |
|
Answer» A generic class is a special kind of class that can handle any types of data. We specify the data types during the object creations of that class. It is declared with the bracket <>. For EXAMPLE CONSIDER the following Comparer class, which has a method that compare TWO value and returns as Boolean OUTPUT. public class Comparer { public bool Compare(Unknown t1, Unknown t2) { if (t1.Equals(t2)) { return true; } else { return false; } } } Comparer oComparerInt = new Comparer(); Console.WriteLine(oComparerInt.Compare(10, 10)); Comparer oComparerStr = new Comparer(); Console.WriteLine(oComparerStr.Compare("jdhsjhds", "10")); A generic class is a special kind of class that can handle any types of data. We specify the data types during the object creations of that class. It is declared with the bracket <>. For example consider the following Comparer class, which has a method that compare two value and returns as Boolean output. public class Comparer { public bool Compare(Unknown t1, Unknown t2) { if (t1.Equals(t2)) { return true; } else { return false; } } } Comparer oComparerInt = new Comparer(); Console.WriteLine(oComparerInt.Compare(10, 10)); Comparer oComparerStr = new Comparer(); Console.WriteLine(oComparerStr.Compare("jdhsjhds", "10")); |
|