Why do I get warning CS8602?
Asked Answered
P

6

9

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

Panek answered 16/1, 2022 at 8:42 Comment(5)
You can try my answer https://mcmap.net/q/108463/-non-nullable-property-must-contain-a-non-null-value-when-exiting-constructor-consider-declaring-the-property-as-nullableSnipe
MethodBase method? = System.Reflection.MethodBase.GetCurrentMethod(); if (method != null) etc.Doubletree
From the documentation, MethodBase.GetCurrentMethod may return null. MethodBase.Name shouldn't be null. So GetCurrentMethod()!.Name ?Mortality
Related to #1799821Tetrahedron
@Tetrahedron sorry I could not find this post from 13 years ago :) Thanks for the referencePanek
E
20

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

Eddins answered 4/4, 2022 at 13:28 Comment(1)
@Jeremy Lakeman was answered first in Jan 31, 2022, see comments above in the Guy Cohen questionTomcat
E
6

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.

Esse answered 31/1, 2022 at 4:4 Comment(0)
B
2

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
Benco answered 18/9, 2023 at 8:59 Comment(0)
A
1

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.

Amphi answered 10/8, 2022 at 19:8 Comment(0)
A
0

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

Assyrian answered 2/1 at 17:49 Comment(0)
J
-2

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.

Joleenjolene answered 16/1, 2022 at 8:54 Comment(3)
I am aware of using? But the same message appears as well :(Panek
As long as its just a warning and you are controlling your code then you should not worry about it that much. The ultimate solution might be either to deactivate the warning or to wrap it in try/catch block. But as I said, if you control your code and you are sure you won't get a null there just go with it as it is.Joleenjolene
This is invalid as an answer, the valid one is the similar one but with the ? right after the call to GetCurrentMethod(), since that's what might return a null, and you can use null-conditional after it to return the Name if it's not null - if GetCurrentMethod returns null you'd get an exception thrown Alternatively, if you know/can guarantee this will never be null, you can use the null-forgiving operator (!) and assign directly into a string variableLefevre

© 2022 - 2024 — McMap. All rights reserved.