C
C#2y ago
jakeam

❔ Why does this code give the CS8604 warning?

I'm confused why the below code gives the CS8604 warning. As far as I am concerned, I am null checking so it shouldn't appear. How can I improve it?
string? myString = null;

var stringIsEmpty = myString == null;

if (!stringIsEmpty)
{
var reversedString = myString.Reverse();
Console.WriteLine(reversedString);
}
string? myString = null;

var stringIsEmpty = myString == null;

if (!stringIsEmpty)
{
var reversedString = myString.Reverse();
Console.WriteLine(reversedString);
}
17 Replies
Thinker
Thinker2y ago
Nullable flow analysis is not smart enough to figure that out when the result is stored in an intermediate variable.
Lexaro
Lexaro2y ago
Nullable flow analysis is not smart enough to figure that out when the result is stored in an intermediate variable.
Thinker
Thinker2y ago
i'm getting more and more convinced you're a bot
Lexaro
Lexaro2y ago
i'm getting more and more convinced you're a bot
Thinker
Thinker2y ago
If you did if (myString != null) or if (myString is not null) then it would pick up on it
Lexaro
Lexaro2y ago
If you did if (myString != null) or if (myString is not null) then it would pick up on it
jakeam
jakeamOP2y ago
In my actual use case I've got some more complicated Boolean expressions and I need to name them something more self-documenting.
Thinker
Thinker2y ago
Well, you can do myString!.Reverse() which will force the compiler to just shut up because you know it won't be null
jakeam
jakeamOP2y ago
For example var turnHasChanged = _turnLastTick != null && _turnLastTick != turnThisTick; Oh, that works!
RubyNovaDev
RubyNovaDev2y ago
@Lexaro I've already had to deal with you once this morning - please clarify what you're doing you have 5 minutes or I will assume you're self botting.
Lexaro
Lexaro2y ago
?????
RubyNovaDev
RubyNovaDev2y ago
why are you copypasting other people's messages?
Lexaro
Lexaro2y ago
I help people who have problems coding
RubyNovaDev
RubyNovaDev2y ago
ok
Lexaro
Lexaro2y ago
ok
RubyNovaDev
RubyNovaDev2y ago
I can see you're just not going to address the actual problem !ban @Lexaro self bot
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server