private static void TestAmbiguousGenerics() { var string1 = "Hello".Foo(s => s + " world"); // works int? n = 123; var string2 = n.Foo(x => (x + 1).ToString()); // compiler error Console.WriteLine("{0} {1}".Fmt(string1, string2)); } public static string Foo<T>(this T obj, Func<T, string> func) where T : class { return obj == null ? "" : func(obj); } public static string Foo<T>(this T? str, Func<T, string> func) where T : struct { return str == null ? "" : func(str.Value); } The compiler can't tell whether I'm calling the first overload of Foo<T> where T is Nullable<int>, or the second overload where T is int. Obviously I could make this work by explicitly calling n.Foo<int>(), but is there a way to make the first overload exclude Nullable<> from the restriction of what T can be?
Nullable<int>.ToString()produces the same result asint.ToString()when the value is not null.