1

Is there a way to limit typing of T to be able to add default values to the function parameters?

public class Identity<T>
{
    public readonly T Seed;
    public readonly T Increment;

    public Identity(T seed = 1, T increment = 1)
    {
        Seed = seed;
        Increment = increment;
    }
}

It is the T seed = 1 that i want to be able to do and maybe limit the T to non decimal numeric types like short, int, long...

Andreas
  • 6,958
  • 10
  • 38
  • 52
  • It feels like this might benefit from some additional abstraction, see a similar sort question/answer: http://stackoverflow.com/questions/3140666/where-t-multiple-classes I do think perhaps you could instead work this a different way, though without seeing how you are using this it is difficult to say exactly what shape that might take. Maybe start with implementing an IKeyField or something? – Daniel Dawes Nov 01 '13 at 17:33
  • 1
    This is not duplicate of that! OP wants to be able to use **default values** in parameter definition of generic class! Although, in fact, the pointed question partially answers that "it cannot be done" because you cannot restrict the T to numerics, it is not a "duplicate enough". – quetzalcoatl Nov 01 '13 at 17:35
  • @quetzalcoatl The question also says, "i want to be able to do and maybe limit the T to non decimal numeric types like short, int, long", which would need to be done in order for the given default argument to be sensible. – Servy Nov 01 '13 at 17:43

2 Answers2

1

You can only set "default" values that can be evaluated at compile-time and can be converted to all possible values of T. One example is using default:

public void Add(T parent = default(T))
{
    ....
}

Which doesn't work for your case, unless you want to treat "0" as a "magic" case and treat it as "1". But how would you implement it without restricting T to numeric values? (which you can't do with generics either)

D Stanley
  • 149,601
  • 11
  • 178
  • 240
0

The actual problem is that there's (probably) no way at all to specify a default value for a parameter of type T..

IF you can do it (I dont remember), than you could only specify:

T param1 = null       // for T: class
T param1 = default(T) // for T: any
// T param1 = new T()    // for T: new()  -- cannot be possible, see Servy's comment

because there are no other "constructors" available for a generic type parameter T!

The compiler treats the T as an Object that has no operations and no constructors and no cast/conversions other than specified by the where clauses and/or the ones always inherited from object. Since you can specify only the above ops/requirements.. I think it cannot be done. Generics are not C++ templates ;)

Servy
  • 202,030
  • 26
  • 332
  • 449
quetzalcoatl
  • 32,194
  • 8
  • 68
  • 107
  • default values must be *literals*. The first two options you listed are literals, and so they'll work fine (although `null` would require the type be nullable, which would mean a class constraint). The third is not, so it won't. There are no other literals that one could assign to `T`, but if any were added to the language, the specs for default arguments need not be changed. – Servy Nov 01 '13 at 17:41
  • thanks! That's true, I didn't pay attention to that as I was thinking in very general terms. I'll put a note about your clarification. – quetzalcoatl Nov 01 '13 at 17:44