Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Identify method to C#8 as a nullable checker? [duplicate]

Tags:

c#

nullable

I often use extension methods to check method arguments

void MyMethod(string? test)
{
    Require.NotNull(test, nameof(test));
    // Same as
    // if (test == null) throw new ArgumentNullExeception(nameof(test))

    ...
}

When enabling C# 8's nullables however, the compiler does not think I've checked the argument for null when using the extension method.

Is there a way to identify the extension method Require.NotNull as a nullable checker?

like image 821
Mike Ward Avatar asked Feb 11 '20 15:02

Mike Ward


People also ask

What is the method of C?

In method C, a racemic compound is resolved into the enantiomers by lipase-catalyzed reaction, and then the enantiomers are applied to the synthesis of the enantiomers of an odorous compound as intermediates or chiral building blocks, respectively.

Is there a method in C?

For Java and C#, there are only methods. For C, there are only functions.

What is function calling C?

Function Calling: A function call is an important part of the C programming language. It is called inside a program whenever it is required to call a function. It is only called by its name in the main() function of a program. We can pass the parameters to a function calling in the main() function.


Video Answer


1 Answers

There is ongoing work in roslyn#36039 to eventually allow authors to annotate their methods in a way that gives back nullable information to the compiler. There are some features currently in preview but I don’t know the exact state.

However, I would question your approach here: If the first thing you do in your method is to check against null and effectively throw an exception, then passing a null value to your method is not a valid input. So your method should not be annotated in a way that it would accept null.

You rather want to mark it so that it does not accept null, so the compiler will properly tell you where you are calling the method with a potential null value.

like image 144
poke Avatar answered Sep 22 '22 12:09

poke