简体   繁体   English

我该如何修复以下 nullreference? 以及如何关闭 nullrefernce(来自<propertygroup> )?</propertygroup>

[英]how i can fix following nullrefernce ? and also how can I turn off nullrefernce (from <propertygroup>)?

public class BlockChain
{   
   public IList<Block>Chain{ get; set; }
}

Non-nullable property 'Chain' must contain a non-null value when exiting constructor.退出构造函数时,不可为 null 的属性“Chain”必须包含非空值。 Consider declaring the property as nullable.考虑将该属性声明为可为空。

Not sure what project you are writing but you could do:不确定你在写什么项目,但你可以这样做:

  1. add <Nullable>disable</Nullable> in.csproj file在.csproj文件中添加<Nullable>disable</Nullable>
  2. add #nullable disable in class在 class 中添加#nullable disable
  3. set Chain as public IList<Block> Chain{ get; set; } = new NameOfClassThatImplementsInterface();Chain设置为public IList<Block> Chain{ get; set; } = new NameOfClassThatImplementsInterface(); public IList<Block> Chain{ get; set; } = new NameOfClassThatImplementsInterface(); (recommended) (推荐的)
  4. set Chain as public IList<Block>? Chain{ get; set; }Chain设置为public IList<Block>? Chain{ get; set; } public IList<Block>? Chain{ get; set; }

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

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