简体   繁体   English

无法将 null 文字转换为不可为 null 的引用类型

[英]Cannot convert null literal to non-nullable reference type

Using C# 10 I have the interface:使用 C# 10 我有界面:

public interface IPluralRuleProvider {
    Boolean TryGetRule(CultureInfo culture, out PluralizationRuleDelegate rule);
}

In a class I am implementing the method and I have:在 class 中,我正在实施该方法并且我有:

Rules = new Dictionary<String, PluralizationRuleDelegate>();

Boolean IPluralRuleProvider.TryGetRule(CultureInfo culture, out PluralizationRuleDelegate rule)
{
    rule = null;

    if (Rules.TryGetValue(culture.Name, out rule))
        return true;    

    if (culture.Parent != null && Rules.TryGetValue(culture.Parent.Name, out rule))
        return true;

    return false;
}

I am getting the warnings:我收到警告:

Cannot convert null literal to non-nullable reference type无法将 null 文字转换为不可为 null 的引用类型
Possible null reference assignment可能的 null 参考分配

I have been trying different versions but I always get some kind of warning.我一直在尝试不同的版本,但总是收到某种警告。

Can I change something to avoid this?我可以改变一些东西来避免这种情况吗?

  • I assume when TryGetRule returns false that the out rule parameter will be assigned to null .我假设当TryGetRule返回false时, out rule参数将分配给null
    • ...in that case you need to add two annotations to out PluralizationRuleDelegate rule : ...在这种情况下,您需要向out PluralizationRuleDelegate rule添加两个注释:
      1. PluralizationRuleDelegate? - ie " rule can be null ". - 即“ rule可以null ”。
      2. [NotNullWhen(true)] - ie "Even though we've declared rule as maybe- null , we pinky-swear promise that when this method returns true that the out rule parameter will not be null . [NotNullWhen(true)] - 即“即使我们已将rule声明为 maybe- null ,我们小指发誓 promise 当此方法返回true时, out rule参数将不是null
        • Note that the C# compiler does not (and cannot , I think?) exhaustively prove your [NotNullWhen(true)] postcondition - so be sure to double-check your logic - consider adding && rule != null checks to every this.Rules.TryGetValue` call-site too.请注意,C# 编译器不会(我认为也不能?)详尽地证明您的[NotNullWhen(true)]后置条件 - 因此请务必仔细检查您的逻辑 - 考虑添加&& rule != null检查每个 this.Rules。 TryGetValue` 调用站点也是如此。

Anyway, change your interface to this:无论如何,将您的interface更改为:

using System.Diagnostics.CodeAnalysis;

public interface IPluralRuleProvider {
  Boolean TryGetRule(CultureInfo culture, [NotNullWhen(true)] out PluralizationRuleDelegate? rule);
}

and implementation:和实施:

using System.Diagnostics.CodeAnalysis;

Boolean IPluralRuleProvider.TryGetRule(CultureInfo culture, [NotNullWhen(true)] out PluralizationRuleDelegate? rule) {

  if (this.Rules.TryGetValue(culture.Name, out rule))
    return true;

  if (culture.Parent != null && this.Rules.TryGetValue(culture.Parent.Name, out rule))
    return true;

  return false;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 Blazor 组件中使用 @ref 时,“无法将 null 文字转换为不可空引用类型” - "Cannot convert null literal to non-nullable reference type" when using @ref in a Blazor component CS8625 无法将空文字转换为 Interlocked.Exchange(ref c, null) 的不可空引用类型警告 - CS8625 Cannot convert null literal to non-nullable reference type warning for Interlocked.Exchange(ref c, null) CS8625 无法将 null 文字转换为 API 调用的不可空引用类型警告,该调用可能预期 null - CS8625 Cannot convert null literal to non-nullable reference type warning for API call that may expect null 无法将 null 转换为“bool”,因为它是不可为 null 的值类型 - Cannot convert null to 'bool' because it is a non-nullable value type 将 null 文字或可能的 null 值转换为不可空类型 - Converting null literal or possible null value to non-nullable type C# 8 中不可为空的引用类型在运行时可以为空吗? - Can a non-nullable reference type in C# 8 be null in runtime? 确定类型引用是否可为空/不可为空 - Determine if type reference is nullable/non-nullable 获取错误“因为它是不可为空的值类型,所以无法转换为null&#39;Point&#39;。” - Get error “Cannot convert to null 'Point' because it is a non-nullable value type.” 为什么在这里出现“因为它是非空值类型,所以无法将null转换为&#39;ScheduleType&#39;”? - Why am I getting “Cannot convert null to 'ScheduleType' because it is a non-nullable value type” here? Mvc ViewBag - 无法将 null 转换为“bool”,因为它是不可为 null 的值类型 - Mvc ViewBag - Cannot convert null to 'bool' because it is a non-nullable value type
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM