Guy Cohen
Guy Cohen

Reputation: 767

Why do I get warning CS8602?

I saw many questions regarding this error, but all/most of them contain complex code or types.

I used this line for many years to prompt the name of the method with an error:

string currentMethod = System.Reflection.MethodBase.GetCurrentMethod().Name;

But now I get this error :

warning CS8602: Dereference of a possibly null reference.

What should I change / Is there another way to get the name of the current method without having this error?

Thanks

Upvotes: 11

Views: 23047

Answers (6)

Fried
Fried

Reputation: 1460

GetCurrentMethod() is declared to be able to return a null value. If you are sure that the value is not null, you may use the null-forgiving "!" operator e.g.

string currentMethod = System.Reflection.MethodBase.GetCurrentMethod()!.Name;

See https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/null-forgiving

Upvotes: 21

Leo Bartkus
Leo Bartkus

Reputation: 1945

In your project file, delete <Nullable>enable</Nullable> to disable nullable reference types.

Upvotes: 0

This is because of null-state analysis, which has become stricter since .NET 6. The Name property of the MethodBase class (the type returned by the GetCurrentMethod method) does not return a null value, but there is one important caveat: if you call GetCurrentMethod at an entry point where the current method does not exist, it may return a null value. And this is the reason why the compiler warns "Possible dereference of null".

There are two ways to solve this problem:

1.The first way (I recommend) is to specify what to do if a null value is returned. This can be done with conditional commands:

        string currentMethod
        if (System.Reflection.MethodBase.GetCurrentMethod().Name is not null)
        {
            currentMethod = System.Reflection.MethodBase.GetCurrentMethod().Name;
        }else
        {
            currentMethod = "Unknown";
        }

or use the Null-Coalescing Operator(more concise way):

string currentMethod = System.Reflection.MethodBase.GetCurrentMethod()?.Name ?? "Unknown";

2.The second way is for when you are sure that the null value will not be returned, but the compiler warns. In this situation, the null-forgiving operator can be used to tell the compiler that the property won't return null:

string methodName = System.Reflection.MethodBase.GetCurrentMethod()!.Name;

3.Or you can use the pragma directive. This directive is used when you work with repeatedly nullable values in a part of your code or when ASP.NET Core features clash with null state analysis, use it to disable this warning in a block of your code:

#pragma warning disable CS8602

And then you can use the

#pragma warning restore CS8602

command to restore it

like:

#pragma warning disable CS8602
string currentMethod = System.Reflection.MethodBase.GetCurrentMethod().Name;
#pragma warning restore CS8602

Upvotes: 2

Keith Banner
Keith Banner

Reputation: 836

If you're okay with sometimes getting a null back you can do this:

string? currentMethod = System.Reflection.MethodBase.GetCurrentMethod()?.Name;

Note the question mark after the method call.

Upvotes: 1

m12lrpv
m12lrpv

Reputation: 1287

While you possibly do have an unhandled null variable or property it can be seen that the CS8602 is prone to false positives. https://github.com/dotnet/roslyn/issues/44805 and https://github.com/dotnet/roslyn/issues/49653

Those are a couple of complex scenarios that people have bothered to report but in my experience even more simple scenarios with very explicit preceding null checks can still result in this warning.

Because efforts to removing this warning can result in you making non-nullable fields nullable just to make the warning go away the solution to CS8602 is often worse than the problem.

The Answer: Downgrade the warning until they fix the false positives.

Upvotes: 6

The problem is quiet clear. The name might be null at some point. Try using ? so the compiler will know that you are aware of a possible null coming from System.Reflection.MethodBase.GetCurrentMethod().Name;

Usage:

string? currentMethod = System.Reflection.MethodBase.GetCurrentMethod().Name;

Documentation: read more here.

Upvotes: -2

Related Questions