简体   繁体   中英

Can my custom method satisfy Pragma Warnings?

For instance, when I do string.IsNullOrWhiteSpace("") , this satisfies the pragma warning:

CS8604: Possible null reference argument

Now, if I had defined an extension method called "".IsNull() , would it be possible to somehow get the IDE/Compiler to recognizer it as a valid handler for CS8604?

public static bool IsNull(this string? s) => string.IsNullOrWhiteSpace(s);

Yes - you want one of the attributes used by null-state static analysis .

In this particular case, I think you want NotNullWhen :

public static bool IsNull([NotNullWhen(false)] this string? s) => 
    string.IsNullOrWhiteSpace(s);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM