Generics is a mixture of generics notation and implementation. I can use generics notation to define a class and methods but if I dont implement the entire class in a generic manor using generic data structures then it does no good.
Thing about the generics classes that are already defined in the system.colloectio.generics name space.
List<string> List<int> List<double> List<MyClass> are all created from the same generic class List<T>
The class does not contain a data structure for every single type under the sun, that is just not possible. In stead it use generic to define a type safe data (such as T[] m_GenericArray) structure with an interface to access it underlying data in a type safe manor. Which by the way is the goal of generics.
You can do the same thing with an object array but the problem with an object array is that it is not type safe you can store any object and many types of objects in a object array, but a generic type safe array you can only store object of Type T in it and the type of data to store passed in the angle brackets.
Looking at the your last post I would not even go so far as to define a generic class that implements a search method. I would have just defined a single method that searches a generic IEnumerable which is an interface that all .net collections implement.
Or just a single methods that search a generic array i.e.
public static class GenericMethods
{
// Performs a linear search on an array of type T
// and return the index of the element if found.
// returns -1 otherwise.
public static int LinearSearch<T>(T[] genericArray, T findValue )
{
}
}
If you implement this method properly it will satisfy all your requirements with out having to define your own custom generic class.
Give the link i mention a good read and see if you van finish the above method.
I will also impletemnt my own version so we can post them for comparison.
Last edited by mmorgan30; February 16th, 2015 at 09:20 PM..
|