Home > Software design >  NET Core (C#) question mark in method parameter
NET Core (C#) question mark in method parameter

Time:11-16

I am new to Net Core, and today i have read the following code :

public void SomeMethod (object? p1, object? p2, object? p3)

Is not clear to me the meaning of question mark in parameter, in Net Framwork a quotation mark after a non nullable type is used to define it as nullable (eg : DateTime?). But object is a nullable type, so i am confused.

What does it mean? Is there a reference where i can get detail of the new syntax without bother this community?

CodePudding user response:

I believe that this is due to changes made in NET 6.

From https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/nullable-reference-types

All project templates starting with .NET 6 (C# 10) enable the nullable context for the project. Projects created with earlier templates don't include this element, and these features are off unless you enable them in the project file or use pragmas.

I suspect that as an object type could be replaced with a any class type that you specifically want to be non-null then this check would also have to apply to it.

CodePudding user response:

The question mark at the end of your datatype in the method signature means the parameter can be null. Here's the C# documentation where you can reference syntax like this with detailed explanations. https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/

  • Related