Skip to main content

How to Pass null to an Overloaded Method in C#

· 7 min read
Jagdish Kumawat
Founder @ Dewiride

When two overloads of a method can both take null, a bare null doesn't tell C# which one you mean, and the call doesn't build. Cast it, as in (string?)null, and the compiler knows.

The C# specification on the null literal: An implicit conversion exists from the null literal to any reference type or nullable value type. So null fits a string? parameter and an int? parameter equally well, and with two such overloads, neither one wins. Microsoft's guide to these errors: These errors indicate there isn't one better overload than others. And the fix: In most of these cases, adding an explicit cast can specify which overload should be chosen.

Syntax​

Syntax
<MethodName>((<ParameterType>)null);
PartWhat it is
<MethodName>A method with more than one overload that can take null
(<ParameterType>)A cast to the parameter type of the overload you mean, such as (string?) or (int?)
nullThe value: no object, or no number

The Example​

A console app called NullOverload has a Display class with two Show overloads: one takes a string?, the other an int?. Each prints its own name and what it got, so the output shows which overload every call picked.

Prerequisites​

  • The .NET 10 SDK. dotnet --version prints a version that starts with 10.0.

Step 1: Create a Console App​

Terminal
dotnet new console -n NullOverload
cd NullOverload
Output
The template "Console App" was created successfully.

Processing post-creation actions...
Restoring /Users/you/source/NullOverload/NullOverload.csproj:
Determining projects to restore...
Restored /Users/you/source/NullOverload/NullOverload.csproj (in 45 ms).
Restore succeeded.

dotnet new console -n NullOverload creates a folder named NullOverload with a project file and a Program.cs, and cd moves into it. Everything this example uses is part of .NET, so there's no package to add. Open the folder in VS Code, or NullOverload.csproj in Visual Studio.

Step 2: Write the Code​

Replace everything in Program.cs. First, two ordinary calls:

Program.cs (ordinary calls)
// Two overloads: one takes text, the other a number.
Display.Show("Asha");
Display.Show(31);

"Asha" is a string, so it goes to Show(string?), and 31 is an int, so it goes to Show(int?).

Then pass null, with a cast:

Program.cs (null with a cast)
// A bare null fits both string? and int?, so Display.Show(null) doesn't build.
// Cast the null to the type of the overload you mean.
Display.Show((string?)null);
Display.Show((int?)null);

The comment explains why there's no Display.Show(null); line: it doesn't build. The compiler stops with error CS0121: The call is ambiguous between the following methods or properties: 'Display.Show(string?)' and 'Display.Show(int?)'. A cast gives the null a type: (string?)null is a string? that holds nothing, which only Show(string?) takes, and (int?)null is an int? for Show(int?).

A variable works too:

Program.cs (a typed variable)
// A variable of that type works the same way.
string? missingName = null;
Display.Show(missingName);

missingName is declared as string?, so the call goes to Show(string?) although its value is null.

Last, the class with the two overloads:

Program.cs (the overloads)
static class Display
{
public static void Show(string? text) =>
Console.WriteLine(text is null ? "Show(string?) got null" : $"Show(string?) got {text}");

public static void Show(int? number) =>
Console.WriteLine(number is null ? "Show(int?) got null" : $"Show(int?) got {number}");
}

Both methods are called Show, and only their parameter types differ: string? and int?.

Step 3: Run It​

Run it in VS Code's terminal, or press Ctrl+F5 in Visual Studio:

Terminal
dotnet run
Output
Show(string?) got Asha
Show(int?) got 31
Show(string?) got null
Show(int?) got null
Show(string?) got null

macOS-style terminal window titled Terminal — NullOverload showing dotnet run printing Show(string?) got Asha, Show(int?) got 31, Show(string?) got null, Show(int?) got null and Show(string?) got null

Every call reached the overload it asked for: (string?)null went to Show(string?), (int?)null to Show(int?), and the string? variable that holds null to Show(string?). The variable's type chose the overload, and its value played no part.

The Complete File​

macOS-style code window titled Program.cs — NullOverload showing calls to Display.Show with Asha, 31, a null cast to string?, a null cast to int? and a string? variable set to null, and a static class Display with two Show overloads, one for string? and one for int?, that print which overload ran

Program.cs
// Two overloads: one takes text, the other a number.
Display.Show("Asha");
Display.Show(31);

// A bare null fits both string? and int?, so Display.Show(null) doesn't build.
// Cast the null to the type of the overload you mean.
Display.Show((string?)null);
Display.Show((int?)null);

// A variable of that type works the same way.
string? missingName = null;
Display.Show(missingName);

static class Display
{
public static void Show(string? text) =>
Console.WriteLine(text is null ? "Show(string?) got null" : $"Show(string?) got {text}");

public static void Show(int? number) =>
Console.WriteLine(number is null ? "Show(int?) got null" : $"Show(int?) got {number}");
}

Common Mistakes​

MistakeWhat happensDo this instead
Passing a bare nullDisplay.Show(null); didn't build: error CS0121: The call is ambiguous between the following methods or properties: 'Display.Show(string?)' and 'Display.Show(int?)'Cast it to the type of the overload you mean: (string?)null or (int?)null
Passing default insteadDisplay.Show(default); failed the same way, with error CS0121Give default a type: default(string) picked Show(string?) and default(int?) picked Show(int?). The guide lists this cause: One or more arguments uses default (not default(T)) and multiple types could be chosen.
Casting to the non-nullable type(int)null didn't build: error CS0037: Cannot convert null to 'int' because it is a non-nullable value typeCast to the nullable type, (int?)null
(string)null with nullable reference types onIt picked Show(string?), with a warning: warning CS8600: Converting null literal or possible null value to non-nullable type.Write (string?)null, which built without a warning

FAQ​

Why does a bare null work with Show(string?) and Show(object?)?​

Because one overload is better there. With those two, Display.Show(null); built and printed Show(string?) ran. Both take null, but a string converts to object and not the other way round, so string? is the closer match. The C# specification calls a type the better conversion target when an implicit conversion from T₁ to T₂ exists and no implicit conversion from T₂ to T₁ exists. Here, T₁ is string? and T₂ is object?.

Can a named argument pick the overload?​

Yes, when the overloads' parameters have different names. Display.Show(text: null); picked Show(string?), and Display.Show(number: null); picked Show(int?).

Conclusion​

When more than one overload can take null, give the null a type: a cast such as (string?)null, a typed variable, default(string) or a named argument. Each one tells the compiler which overload you mean.

Additional Resources​