[英]How to set null object A in List<A>() C#
I have a class A like this: 我有这样的A类:
public class Article
{
public Tag Test1 { get; set; } = new Tag();
}
public class Tag
{
public List<News> Test{ get; set; } = new List<News>();
}
I have a list like this: 我有一个这样的清单:
News 1
Tag 1{
News 1
**Tag** {
News 1
Tag {....}
News 4
Tag {....}
News 5
Tag {....}
}
News 4
Tag {....}
News 5
Tag {....}
}
News 2
News 3
I want to set null " Tag " 2nd on that list = null to avoid loop infinitely. 我想在该列表上将null“ Tag ” 2nd设置为null以避免无限循环。 like this:
像这样:
News 1
Tag 1{
News 1
Tag = null
News 4
Tag = null
News 5
Tag = null
}
News 2
News 3
How to implement that. 如何实施。
Rather than checking for null, you really should be checking for Test1.Count > 0
in your code. 而不是检查null,实际上应该在代码中检查
Test1.Count > 0
。 Then make sure no one adds any tags to the second level collections. 然后确保没有人将任何标签添加到第二级集合。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.