I know how you can use List<T>
for example and decide what that collection is holding. That's where T comes in. But I'm not sure I understand the brackets fully.
If I create a class...
class MyClass<int> { }
Or instead of int I could use T or object or string or whatever. What does that mean? Does it turn into a collection automatically?
Generic classes allow class members to use type parameters. They are defined in the same way as generic methods, by adding a type parameter after the class name.
class Point<T>
{
public T x, y;
}
To instantiate an object from the generic class the standard notation is used, but with the type argument specified after both class names. Note that in contrast to generic methods, a generic class must always be instantiated with the type argument explicitly specified.
Point<short> p = new Point<short>();
Reference: http://www.pvtuts.com/csharp/csharp-generics
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With