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>();