© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•2y ago•
8 replies
Jason_Bjorn

✅ 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));
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));
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

❔ StructureMap.StructureMapException: StructureMap Exception Code: 207
C#CC# / help
3y ago
difference between `struct` and `ref struct`
C#CC# / help
12mo ago
❔ Making properties accessible by both static and non-static methods
C#CC# / help
4y ago
readonly struct and readonly struct instance members (optimization?)
C#CC# / help
4y ago