I have a class that currently has several methods that take integer parameters. These integers map to operations that the application can perform. I'd like to make the class generic so that the consumers of the class can provide an enum type that they have with all the operations in it, then the methods will take parameters of that enum type. However, I want them to be able to not specify a generic type at all, and have it default back to integers with no change in syntax from the current way. Is this possible?
To construct a generic type from the generic type definition for a nested type, call the MakeGenericType method with the array formed by concatenating the type argument arrays of all the enclosing types, beginning with the outermost generic type, and ending with the type argument array of the nested type itself, if it ...
Use the IsGenericType property to determine whether the type is generic, and use the IsGenericTypeDefinition property to determine whether the type is a generic type definition. Get an array that contains the generic type arguments, using the GetGenericArguments method.
If we want the data to be of int type, the T can be replaced with Integer, and similarly for String, Character, Float, or any user-defined type. The declaration of a generic class is almost the same as that of a non-generic class except the class name is followed by a type parameter section.
So... why not use simple inheritance? Like:
class MyGenericClass<T> { } class MyGenericClass : MyGenericClass<int> { }
This way you can write both ways:
var X = new MyGenericClass<string>(); var Y = new MyGenericClass(); // Is now MyGenericClass<int>
You can't do it in the definition of the class:
var foo = new MyGenericClass(); // defaults to integer... this doesn't work var bar = new MyGenericClass<MyEnum>(); // T is a MyEnum
If really value the implicitness of the default type being int, you'll have to do it with a static factory method, although I don't see the value of it.
public class MyGenericClass<T> { public static MyGenericClass<T> Create() { return new MyGenericClass<T>(); } public static MyGenericClass<int> CreateDefault() { return new MyGenericClass<int>(); } }
See below for how you really don't benefit from the above.
var foo = MyGenericClass<MyEnum>.Create(); var bar1 = MyGenericClass.CreateDefault(); // doesn't work var bar2 = MyGenericClass<int>.CreateDefault(); // works, but what's the point
If you want to take it even farther, you can create a static factory class that will solve this, but that's an even more ridiculous solution if you're doing it for no other reason than to provide a default type:
public static class MyGenericClassFactory { public static MyGenericClass<T> Create<T>() { return new MyGenericClass<T>(); } public static MyGenericClass<int> Create() { return new MyGenericClass<int>(); } } var foo = MyGenericClassFactory.Create(); // now we have an int definition var bar = MyGenericClassFactory.Create<MyEnum>();
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