Код: Выделить всё
public class Error
{
public string Message = "...";
public static implicit operator bool(Error? err) => err is not null;
}
Код: Выделить всё
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); //
Подробнее здесь: [url]https://stackoverflow.com/questions/79195410/c-sharp-help-compiler-to-infer-nullability[/url]
Мобильная версия