✅ Two methods: struct and non-struct

Why does b.F(42) call the 2nd overload? 42 is a value-type which is what structs are?

public class Base
{
  public virtual void F<T>(T? t) where T : struct { }
  public virtual void F<T>(T? t) { }
}

Base b = new();
int? nullInt = null;
int? nonNullNullableInt = 42;

// These call the 1st overload.
b.F(nullInt);
b.F(nonNullNullableInt);
b.F(default(int?));

// These call the 2nd overload.
b.F(42);
b.F("Hello");
b.F(default(int));
Was this page helpful?