I do my best to check null values. But often, I try this with the is-Operator. While this, I expect, he will give true back only, if operand is a not-null value.
Example:
EndPoint endPoint = new();
if (endPoint is IPEndPoint)
DoSomething(endPoint as IPEndPoint); // The "as" expressions give the CS8602 warning
...
void DoSomething(IPEndPoint endPoint)
{
...
}
With this, I get a CS8602 compiler warning, but endPoint is a IPEndPoint and the as expression can't be null. So IMHO, the compiler warning is wrong. How can I mute it for before checks with the is-Operator only?
(Maybe inside the .editorconfig anyhow?)
CodePudding user response:
is and as are two separate casting operations and the compiler tries to cast and validate both of them, so it's reasonable to say warning CS8602.
As @Adassko said in the comment the best way is to cast the object once and use the casting result (if castable):
EndPoint endPoint = new();
if (endPoint is IPEndPoint endpoint)
DoSomething(endpoint);
