I want to be able to call the generic foo method which has a Nullable param. When the body of the Foo method calls the Foo(), it goes recursive, resulting in a stack overflow (without the .com). Is there a way to have the call in the if statement call the correct overload with the runtime type?
I know changing the names would do it and I understand why the recursion is happening, but don't know of another way to write this.
Thanks for looking.
class Program {
static void Main(string[] args) {
int? x = 5;
int y = 10;
Foo(x, y);
}
static void Foo<T>(Nullable<T> arg, T defaultValue) where T : struct {
if (arg.HasValue)
Foo(arg.Value, defaultValue);
}
static void Foo(int arg, int defaultValue) {
Console.WriteLine(string.Format("I'm an int arg={0}, default={1]}", arg, defaultValue));
}
static void Foo(string arg, int defaultValue) {
Console.WriteLine(string.Format("I'm an string arg={0}, default={1]}", arg, defaultValue));
}
static void Foo(bool arg, int defaultValue) {
Console.WriteLine(string.Format("I'm an double arg={0}, default={1]}", arg, defaultValue));
}
}