Skip to main content

Fixed: warning CS8621: Nullability of reference types in return type doesn't match the target delegate

· 6 min read
Jagdish Kumawat
Founder @ Dewiride

Adding a ? to a method's return type is the usual answer to "this can be null". Here it moved the warning to another line: warning CS8621: Nullability of reference types in return type of '...' doesn't match the target delegate '...' (possibly because of nullability attributes). The method was being handed to a library's delegate, and the delegate had the final say.

What I Was Doing​

I was fixing another warning in a small Microsoft Teams agent, built in C# with the Microsoft 365 Agents SDK on .NET 10. Its Cancel handler returns null on purpose, meaning "no answer", and that null gave warning CS8625: Cannot convert null literal to non-nullable reference type. So I made the return type nullable:

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

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

What I Expected​

A clean build: the method now says it may return null, and it does.

What I Got​

CS8625 was gone, and a new warning pointed at line 29, the line in the constructor that passes CancelAsync to OnSubmit:

Terminal
dotnet build
Output
Determining projects to restore...
All projects are up-to-date for restore.
/Users/you/SupplyDesk/SupplyDeskAgent.cs(29,55): warning CS8621: Nullability of reference types in return type of 'Task<TaskModuleResponse?> SupplyDeskAgent.CancelAsync(ITeamsTurnContext turnContext, ITurnState turnState, TaskModuleRequest request, CancellationToken cancellationToken)' doesn't match the target delegate 'TaskSubmitHandler' (possibly because of nullability attributes). [/Users/you/SupplyDesk/SupplyDesk.csproj]
SupplyDesk -> /Users/you/SupplyDesk/bin/Debug/net10.0/SupplyDesk.dll

Build succeeded.

/Users/you/SupplyDesk/SupplyDeskAgent.cs(29,55): warning CS8621: Nullability of reference types in return type of 'Task<TaskModuleResponse?> SupplyDeskAgent.CancelAsync(ITeamsTurnContext turnContext, ITurnState turnState, TaskModuleRequest request, CancellationToken cancellationToken)' doesn't match the target delegate 'TaskSubmitHandler' (possibly because of nullability attributes). [/Users/you/SupplyDesk/SupplyDesk.csproj]
1 Warning(s)
0 Error(s)

Time Elapsed 00:00:01.30

A zsh terminal where dotnet build of SupplyDesk prints warning CS8621: Nullability of reference types in return type of Task of TaskModuleResponse? SupplyDeskAgent.CancelAsync doesn't match the target delegate TaskSubmitHandler, possibly because of nullability attributes, then Build succeeded with 1 warning

What I Checked​

What the warning compares. Two types: the method's return type, Task<TaskModuleResponse?>, and the return type of the delegate it's passed to, TaskSubmitHandler. OnSubmit only accepts a TaskSubmitHandler, so C# turns the method into one, and checks that the two agree on null.

The delegate's declaration. In the SDK, TaskSubmitHandler returns Task<Microsoft.Teams.Apps.TaskModules.TaskModuleResponse>, with no ?: anyone calling a TaskSubmitHandler is told the response is never null. My method now said it could be.

What Microsoft's documentation says. The C# reference puts CS8621 among the warnings for Mismatch in nullability declaration: Or a delegate type and the expression for that delegate differ. Its advice: To fix these warnings, update the appropriate declaration.

Why It Happens​

A method passed as a delegate has to keep the delegate's promises. TaskSubmitHandler promises a non-null response, and a method returning Task<TaskModuleResponse?> doesn't. "Update the appropriate declaration" would mean changing either side, and the delegate's side belongs to the SDK, so only my side can change, back to the delegate's exact type.

The Fix​

Match the delegate's return type exactly, and mark the one intended null with the null-forgiving operator, !:

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 matches TaskSubmitHandler again, so CS8621 is gone, and null! keeps CS8625 away. That's right here only because the SDK's code accepts a null response: it sends Teams an empty HTTP 200 answer, and Teams closes the dialog.

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.14

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

Common Mistakes​

MistakeWhat happensDo this instead
Adding ? to a method that's passed to someone else's delegateWarning CS8621 on the line that passes itKeep the delegate's exact return type
Using null! when the receiver doesn't handle nullA NullReferenceException later, at run timeReturn a real value instead

Troubleshooting​

SymptomLikely causeFix
warning CS8621 on a line that registers a handlerThe handler's return type differs from the delegate's in nullabilityGive it the delegate's return type
warning CS8622 insteadA parameter's nullability differs, not the return typeMatch the delegate's parameter types
CS8625 comes back after the fixThe null lost its !Write null!

FAQ​

What does "possibly because of nullability attributes" mean?​

Nullability can come from attributes, such as [return: MaybeNull] on a method, as well as from ?, and the message allows for both. Here it was the ?.

Is it safe to ignore CS8621?​

It doesn't stop the build. But the warning points at a place where null could reach code that was told it never would, so it's worth an exact fix.

Could I declare my own delegate instead?​

Only for your own code. The SDK's OnSubmit takes its own TaskSubmitHandler, so the method has to match that one.

Conclusion​

CS8621 says a method and the delegate it's passed as disagree about null in the return type. When the delegate comes from a library, keep its exact return type, and if you need to return null on purpose, and the library handles it, write null!.

Additional Resources​