In C++ templates, one can specify that a certain type parameter is a default. I.e. unless explicitly specified, it will use type T.
Can this be done or approximated
Unfortunately C# does not support what you are trying to do. It would be a difficult feature to implement given that the default type for a parameter would have to adhere to the generic constraints and would most likely create headaches when the CLR tried to ensure type-safety.
Subclassing is the best option.
I would subclass your main generic class:
class BaseGeneric<T,U>
with a specific class
class MyGeneric<T> : BaseGeneric<T, string>
This makes it easy to keep your logic in one place (the base class), but also easy to provide both usage options. Depending on the class, there is probably very little extra work needed to make this happen.
One solution is subclassing. Another one I would use instead, is factory methods (combined with var keyword).
public class MyTemplate<T1,T2>
{
public MyTemplate(..args..) { ... } // constructor
}
public static class MyTemplate{
public static MyTemplate<T1,T2> Create<T1,T2>(..args..)
{
return new MyTemplate<T1, T2>(... params ...);
}
public static MyTemplate<T1, string> Create<T1>(...args...)
{
return new MyTemplate<T1, string>(... params ...);
}
}
var val1 = MyTemplate.Create<int,decimal>();
var val2 = MyTemplate.Create<int>();
In the above example val2
is of type MyTemplate<int,string>
and not a type derived from it.
A type class MyStringTemplate<T>:MyTemplate<T,string>
is not the same type as MyTemplate<T,string>
.
This could pose some problems in certain scenarios.
For instance you can't cast an instance of MyTemplate<T,string>
to MyStringTemplate<T>
.
you can also create a class Overload like so
public class MyTemplate<T1, T2> {
public T1 Prop1 { get; set; }
public T2 Prop2 { get; set; }
}
public class MyTemplate<T1> : MyTemplate<T1, string>{}
C# does not support such a feature.
As you said, you can subclass it (if it's not sealed, and duplicate all constructor declarations) but it's a completely different thing.