Skip to main content

Fixed: warning CS8625: Cannot convert null literal to non-nullable reference type

· 7 min read
Jagdish Kumawat
Founder @ Dewiride

Returning null was the whole point of the method: no answer, so the dialog closes. C# still warned, warning CS8625: Cannot convert null literal to non-nullable reference type, because the type the method had to return says it's never null. One character in the right place fixed it, and the fix that looked more natural only swapped this warning for another one.

What I Was Doing​

I was building a small Microsoft Teams agent in C# with the Microsoft 365 Agents SDK, on .NET 10. Its dialog has a Cancel button, and the SDK closes a dialog without a message when the handler gives back no answer at all. So the Cancel handler returned null:

SupplyDeskAgent.cs
// In the constructor:
TeamsExtension.TaskModules.OnSubmit("cancel", CancelAsync);

// task/submit from Cancel: no answer, so Teams closes the dialog and shows nothing.
private Task<TaskModuleResponse> CancelAsync(ITeamsTurnContext turnContext, ITurnState turnState, TaskModuleRequest request, CancellationToken cancellationToken)
{
return Task.FromResult<TaskModuleResponse>(null);
}

OnSubmit takes a handler of the SDK's delegate type TaskSubmitHandler, which returns Task<TaskModuleResponse>. The project had nullable reference types turned on: dotnet new writes <Nullable>enable</Nullable> into the project file.

What I Expected​

A clean build. The SDK's own code accepts null there: it sends Teams an HTTP 200 answer with no body, which Teams reads as "nothing to show".

What I Got​

A warning on the return line:

Terminal
dotnet build
Output
Determining projects to restore...
All projects are up-to-date for restore.
/Users/you/SupplyDesk/SupplyDeskAgent.cs(94,52): warning CS8625: Cannot convert null literal to non-nullable reference type. [/Users/you/SupplyDesk/SupplyDesk.csproj]
SupplyDesk -> /Users/you/SupplyDesk/bin/Debug/net10.0/SupplyDesk.dll

Build succeeded.

/Users/you/SupplyDesk/SupplyDeskAgent.cs(94,52): warning CS8625: Cannot convert null literal to non-nullable reference type. [/Users/you/SupplyDesk/SupplyDesk.csproj]
1 Warning(s)
0 Error(s)

Time Elapsed 00:00:02.09

A zsh terminal where dotnet build of SupplyDesk prints warning CS8625: Cannot convert null literal to non-nullable reference type for SupplyDeskAgent.cs line 94, column 52, then Build succeeded with 1 warning and 0 errors

It's only a warning, so the agent still built and ran. But a warning you leave in place hides the next one, and this one points at a real question: is null allowed here or not?

What I Checked​

Where the warning points. Column 52 of line 94 is the null inside Task.FromResult<TaskModuleResponse>(null). With nullable reference types on, TaskModuleResponse without a ? means "never null", so Task.FromResult<TaskModuleResponse> asks for a value that isn't null, and gets the null literal.

What Microsoft's documentation says. The C# reference lists CS8625 among the warnings for Possible null assigned to a nonnullable reference: The compiler emits these warnings when you attempt to assign an expression that is maybe-null to a variable that is nonnullable. It gives the ways out: make the target nullable with ?, check for null first, or tell the compiler it's fine.

Whether null is really fine here. It is, on purpose. The SDK's code turns a null answer into an empty 200, and in Teams, selecting Cancel closed the dialog with nothing shown. So the value is right; only the type says otherwise.

Why It Happens​

The handler's return type, Task<TaskModuleResponse>, comes from the SDK's delegate, which declares the response as not nullable. My code returned the null literal into that type, and the compiler can't know that the code on the other side handles null anyway.

The Fix​

Make the return type nullable, then? That was my first try, and it doesn't work here:

SupplyDeskAgent.cs (first try)
private Task<TaskModuleResponse?> CancelAsync(ITeamsTurnContext turnContext, ITurnState turnState, TaskModuleRequest request, CancellationToken cancellationToken)
{
return Task.FromResult<TaskModuleResponse?>(null);
}

CS8625 went away, and the line that registers the handler got warning CS8621: the method no longer matches the SDK's delegate, which you can't change. Fixed: warning CS8621: Nullability of reference types in return type doesn't match the target delegate follows that one.

What fits is the one Microsoft's reference describes for exactly this: the null-forgiving operator, !, after null. From the operator's page: By using the null-forgiving operator, you inform the compiler that passing null is expected and shouldn't generate a warning.

SupplyDeskAgent.cs
// task/submit from Cancel: no answer, so Teams closes the dialog and shows nothing.
// The handler's type promises a response, but the SDK accepts null as "no answer": null! says that's intended.
private Task<TaskModuleResponse> CancelAsync(ITeamsTurnContext turnContext, ITurnState turnState, TaskModuleRequest request, CancellationToken cancellationToken)
{
return Task.FromResult<TaskModuleResponse>(null!);
}

The method keeps the delegate's exact type, and null! tells the compiler that this null is meant. The comment says why, for the next person who reads it.

After the Fix​

Terminal
dotnet build
Output
Determining projects to restore...
All projects are up-to-date for restore.
SupplyDesk -> /Users/you/SupplyDesk/bin/Debug/net10.0/SupplyDesk.dll

Build succeeded.
0 Warning(s)
0 Error(s)

Time Elapsed 00:00:01.38

A zsh terminal where dotnet build of SupplyDesk restores, builds SupplyDesk.dll and prints Build succeeded with 0 warnings and 0 errors

No warnings, and nothing changed at run time: ! exists only for the compiler. In Teams, Cancel still closed the dialog with nothing shown.

Common Mistakes​

MistakeWhat happensDo this instead
Changing the return type to Task<TaskModuleResponse?>CS8625 goes, and warning CS8621 appears where the method is passed to the SDKKeep the delegate's type and return null!
Turning nullable warnings off for the projectEvery other null warning disappears tooFix the one line
Writing null! where null would break the code that receives itThe warning is gone, and the crash comes at run timeUse it only when the receiver handles null

Troubleshooting​

SymptomLikely causeFix
warning CS8625 on a return nullThe return type has no ? and nullable reference types are on? on the type if you own it, otherwise null!
warning CS8621 after adding ?The method's type no longer matches a delegateGo back to the delegate's type and use null!
No warning in an older projectThat project doesn't have <Nullable>enable</Nullable>Nothing to fix

FAQ​

Does null! change what the program does?​

No. It's an instruction to the compiler only. The C# reference: The null-forgiving operator has no effect at run time.

When should I make the type nullable instead?​

When you own the type: your own method or property that really may hold null. Then ? documents it for everyone who calls it. Here the type belongs to the SDK's delegate, so ? had nowhere to go.

Why did the SDK make the response non-nullable if null works?​

That's the SDK's choice in its delegate. Its code handles null as "no answer", so null! is the way to say that from your side.

Conclusion​

CS8625 says a null literal is going where the type says "never null". When the null is intended and the code that receives it handles it, as with an Agents SDK dialog handler that means "no answer", write null! and leave a comment. Making the return type nullable only moves the problem to the delegate.

Additional Resources​