Is it possible to specify my own default object instead of it being null? I would like to define my own default properties on certain objects.
For example, if I have an object foo with properties bar and baz, instead of default returing null, I’d like it to be an instance of foo with bar set to ‘abc’ and baz set to ‘def’ — is this possible?
You have to use a constructor to get that functionality. So, you can’t use default.
But if your goal is to ensure a certain state of the passed type in a generic class, there may still be hope. If you want to ensure the passed type is instanti-able, use a generic constraint. The new() constraint. This ensures that there is a public parameter-less constructor for type T.
Unfortunately, you can’t use this to get constructors that have parameters, only parameter-less constructors.