c# - Rider Bug - C# 8 Nullable 引用参数上的扩展方法显示警告
问题描述
已解决 Turns our Rider (2019.1) 错误地显示此警告,但没问题
dotnet build
。它已在 v2019.2 中修复
我有一个可为空的属性MainBgColor
public class Provider
{
public string? MainBgColor { get; set; }
}
我有一个扩展方法,string.Empty
如果输入为空则返回。
public static string EmptyIfNull(this string input) => input ?? string.Empty;
但是当我尝试使用它时
var provider = GetTheProviderSomehow();
var logo = provider.MainBgColor.EmptyIfNull();
我收到 c#8 编译器生成的警告
[CS8604] Possible null reference argument for parameter 'input' in 'string ProviderExtensions.EmptyIfNull(string input)'.
我的问题是,这会很好,因为我正在检查 null(在扩展方法内)而没有启用可为空的引用类型功能。为什么它还在抱怨?
如果我将扩展方法更改为允许nullable
string?
- 那么编译器认为它不是provider.Logo
.
这是 C#8 可空引用类型的预期功能吗?我不应该对其中一种可为空的类型使用扩展方法吗?
解决方案
您需要将扩展方法声明为接受可为空的字符串。
推荐阅读
- macos - 我在我无法理解的 mac 上有一个 base64 电话登录
- javascript - Angular 6 视图未更新为页面加载时的正确数据
- python - Python中的正弦函数作为泰勒级数逼近
- javascript - 在 regex max 之后拆分而不丢失分隔符
- java - 通过名称和参数在 selenium 中查找定位器
- r - R与多列上一列的差异
- sql - 具有嵌套连接的表别名规则
- javascript - 3 While 循环变成一个循环?
- java - 如何销毁 log4j 记录器实例
- ember.js - Ember 通过链接呈现 html,但通过在浏览器中输入 url 呈现 json