如果輸入不為空,我如何告訴編譯器以下擴展方法回傳不為空?
public static string? SomeMethod(this string? input)
{
if (string.IsNullOrEmpty(input))
return input;
// Do some work on non-empty input
return input.Replace(" ", "");
}
uj5u.com熱心網友回復:
使用以下屬性:
[return: NotNullIfNotNull("input")]
public static string? SomeMethod(this string? input)
{
...
}
進一步閱讀:C# 編譯器解釋的空狀態靜態分析的屬性
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/442704.html
下一篇:無法轉換源型別T?以T型為目標?
