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));
}
}
Generics are a compile time feature, overload resolution has been decided before the program ever runs. If you want to, as you say, force a runtime decision, you could employ
dynamicto achieve that goal (in C# 4+). Short of that, you simply must cast to the applicable type between int and bool otherwise.But beware! This only solves your problem for
intandbool. Other types could be passed to the generic method, and those will continue to resolve to the generic method on the subsequent call.Follow your initial judgment, if you want to support
int,string, andbool, simply have those overloads. If you want to support any given T, create an overload for T.Overload resolution will still pick
int,bool,string, orT?overloads if they are applicable based upon compile time information. All other calls, and the call witharg.Value, will go to this new overload.