C# 创建可为空的类
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9609715/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Creating nullable class
提问by iefpw
How do I create a nullable (?) class in C#? Like:
如何?在 C# 中创建可空 ( ) 类?喜欢:
public class Hello1 {
public int Property1 { get; set; }
}
public class Hello2 {
Public Hello1? Property2 { get; set; } //Hello1 should work when it has "?"
}
I want to make Hello1class to be able to take the form Hello1?if needed.
如果需要,我想Hello1上课以便能够采取这种形式Hello1?。
采纳答案by pfries
You don't need to create a nullable type for reference types. They're already nullable. You only need to do this for Value types like int, bool, decimal, etc...
您不需要为引用类型创建可为空类型。它们已经可以为空了。您只需要对 int、bool、decimal 等值类型执行此操作...
回答by Ry-
You don't need to; reference types can already be null. As for structures, simply appending a ?willwork. So just remove the ?, and check for nullusing == null, as usual.
你不需要;引用类型已经可以是null. 至于结构,简单地追加一个?意志的工作。因此,只需像往常一样删除?, 并检查是否null使用== null。
回答by Matt Grande
Hello1is nullable already. You don't need the ?on your property.
Hello1已经可以为空了。你不需要?你的财产。
Edit- I suspect you might be trying to set Hello1.Property1to null, and you're hoping that making Hello1nullable will allow that. It won't. You need to mark Property1as nullable if you're doing that.
编辑- 我怀疑您可能试图设置Hello1.Property1为 null,并且您希望使Hello1nullable 允许这样做。不会。Property1如果你这样做,你需要标记为可空。
回答by Rune FS
All classes are nullable. nullable is for Value types that can't be null
所有类都可以为空。nullable 用于不能为 null 的值类型
回答by Botz3000
Hello1is already a reference type, so it can be nullalready.
The nullable syntax "classname?" can only be applied to ValueTypes, such as int, DateTime, floatetc., which can not be nullusually.
Hello1已经是一个引用类型,所以它可以null已经是。
可为空的语法“类名?” 只能被应用到值类型,例如int,DateTime,float等等,这不可能是null通常。
回答by Mattias ?slund
Just remove the questionmark. All classes are nullable by default. Only value-type objects (like struct) need to be explicitly made nullable.
把问号去掉就好了。默认情况下,所有类都可以为空。只有值类型对象(如 struct)需要显式设置为可为空。
回答by Obaid
Please remember, there are two kind of datatypes in C# or any programming language, namely Value Types (e.g. int, float, bool) and Reference Types (all the user defined classes and string data type).
请记住,在 C# 或任何编程语言中有两种数据类型,即值类型(例如 int、float、bool)和引用类型(所有用户定义的类和字符串数据类型)。
Value Types get a memory location assigned as soon as they are created. So they must hold a value, whereas reference type only get hold of a memory location when they are explicitly created using the newkeyword.
值类型在创建后立即获得分配的内存位置。所以它们必须持有一个值,而引用类型只有在使用new关键字显式创建时才能持有内存位置。
So all reference types are nullable by-default as long as they are not referring a memory location. And we can always assign them null.
因此,只要引用类型不引用内存位置,默认情况下所有引用类型都是可为空的。我们可以随时分配它们null。
To make Value Types nullable, we create a wrapper around existing value type by appending a question mark ?next to it. This way a Value Type becomes nullable in C#.
为了使值类型可以为空,我们通过在现有值类型?旁边附加一个问号来创建一个包装器。这样,值类型在 C# 中变为可以为空。

