Is there a reasonable approach to default parameters in C # Generics?

In C ++ templates, you can indicate that a parameter of a certain type is the default value. That is, if not explicitly specified, it will use type T.

Can this be done or come closer to C #?

I am looking for something like:

public class MyTemplate<T1, T2=string> {} 

So, an instance of a type that does not explicitly indicate T2 :

 MyTemplate<int> t = new MyTemplate<int>(); 

It would be essential:

 MyTemplate<int, string> t = new MyTemplate<int, string>(); 

Ultimately, I consider the case when there is a template that is pretty widely used, but I am considering expanding with an additional type parameter. I could subclass, I think, but I was curious if there were any other options in this vein.

+64
generics c # types parameters default
Apr 01 '09 at 23:51
source share
5 answers

A subclass 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 (base class), but it is also easy to provide both use cases. Depending on the class, very little extra work may be required to make this happen.

+59
Apr 01 '09 at 23:53
source share

One solution is to subclass. Another that I would use is factory methods (combined with the 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 the type obtained from it.

The class MyStringTemplate<T>:MyTemplate<T,string> type class MyStringTemplate<T>:MyTemplate<T,string> does not match the type MyTemplate<T,string> . This can create certain problems in certain scenarios. For example, you cannot overlay an instance of MyTemplate<T,string> on MyStringTemplate<T> .

+12
Dec 15 '14 at 16:34
source share

C # does not support such a function.

As you said, you can subclass it (if it is not sealed and duplicate all constructor declarations), but this is a completely different thing.

+6
Apr 01 '09 at 23:54
source share

you can also create an overload class as such

 public class MyTemplate<T1, T2> { public T1 Prop1 { get; set; } public T2 Prop2 { get; set; } } public class MyTemplate<T1> : MyTemplate<T1, string>{} 
+6
Dec 02 '15 at 2:23
source share

Unfortunately, C # does not support what you are trying to do. It would be difficult to implement, given that the default type for a parameter would have to comply with general restrictions and would most likely create headaches when the CLR tried to ensure type safety.

+1
Apr 01 '09 at 23:57
source share



All Articles