5

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

Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
Guy Cohen
  • 689
  • 1
  • 9
  • 25

4 Answers4

14

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

Fried
  • 1,323
  • 12
  • 21
3

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.

m12lrpv
  • 997
  • 2
  • 11
  • 18
0

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.

Keith Banner
  • 602
  • 1
  • 10
  • 15
-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.

  • I am aware of using? But the same message appears as well :( – Guy Cohen Jan 16 '22 at 08:57
  • 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. – Alex Florin Ciuciulete Jan 16 '22 at 08:59
  • 3
    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 variable – Javier Rapoport Feb 28 '23 at 04:27