I have some methods like the following:
static public int GetInt(int _default = 0)
{
// do work, return _default if error
}
static public int? GetInt(int? _default = null)
{
// do work, return _default if error
}
I was hoping when using these methods as overrides that the method would be chosen also based on the variable I am using them to assign to:
int value1 = GetInt();
int? value2 = GetInt();
However this results in an “Ambiguous Invocation” error.
What can I do to avoid this?
The problem is that the return type is not part the signature of a method in C#. So you have to name the methods differently. Also, don’t use underscore in parameter names.
To quote MSDN on how method signatures work:
This is a way to resolve it. As you return the default of the type, you can just make it generic and use
default(T).