最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

C# help compiler to infer nullability - Stack Overflow

matteradmin7PV0评论

Conside this simple class in C# (nullability feature enabled):

public class Error
{
    public string Message = "...";

    public static implicit operator bool(Error? err) => err is not null;
}

The implicit bool operator allows me to simplify conditional checks like this:

var err = GetSomeError();

// scenario 1 (without implicit operator)
if (err is not null)
{
    Console.WriteLine(err.Message);
}

// scenario 2 (with implicit operator)
if (err)
{
    Console.WriteLine(err.Message); // <--- complains about nullability
}

However in scenario 2, the compiler doesn't understand that err will never be null there, although the sole purpose of that implicit operator is to check for nullability, thus the execution will only get inside that if statement block when err is not null. However compiler does not recognize this and complains, forcing me to use the bang operator: err!.Message. Bang operator is not the worst solution, however, after codebase lives long enough to see several rounds of refactoring, I suddenly see redundant bang operators which wouldn't be there if not this problem, this wouldn't be too bad at first glance, however there's a risk of bang operator obfuscating nullability areas (where refactoring changed the code logic).

Is there any way to tell the compiler about this? Maybe via some method/class attributes, etc. ?

Conside this simple class in C# (nullability feature enabled):

public class Error
{
    public string Message = "...";

    public static implicit operator bool(Error? err) => err is not null;
}

The implicit bool operator allows me to simplify conditional checks like this:

var err = GetSomeError();

// scenario 1 (without implicit operator)
if (err is not null)
{
    Console.WriteLine(err.Message);
}

// scenario 2 (with implicit operator)
if (err)
{
    Console.WriteLine(err.Message); // <--- complains about nullability
}

However in scenario 2, the compiler doesn't understand that err will never be null there, although the sole purpose of that implicit operator is to check for nullability, thus the execution will only get inside that if statement block when err is not null. However compiler does not recognize this and complains, forcing me to use the bang operator: err!.Message. Bang operator is not the worst solution, however, after codebase lives long enough to see several rounds of refactoring, I suddenly see redundant bang operators which wouldn't be there if not this problem, this wouldn't be too bad at first glance, however there's a risk of bang operator obfuscating nullability areas (where refactoring changed the code logic).

Is there any way to tell the compiler about this? Maybe via some method/class attributes, etc. ?

Share Improve this question asked Nov 16, 2024 at 15:01 chainerltchainerlt 2673 silver badges11 bronze badges 2
  • You could also add public override string ToString() => Message; then you could simply write if (err) { Console.WriteLine(err); } – Olivier Jacot-Descombes Commented Nov 16, 2024 at 15:29
  • I strongly resent your attempts to turn C# into JavaScript
Post a comment

comment list (0)

  1. No comments so far