C#中用户定义类的默认值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1920116/
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
Default value for user defined class in C#
提问by Benny
I see some code will return the default value, so I am wondering for a user defined class, how will the compiler define its default value?
我看到一些代码会返回默认值,所以我想知道对于用户定义的类,编译器将如何定义其默认值?
采纳答案by dxh
To chime in with the rest, it will be null
, but I should also add that you can get the default value of any type, using default
与其余部分一致,它将是null
,但我还应该补充一点,您可以使用default
default(MyClass) // null
default(int) // 0
It can be especially useful when working with generics; you might want to return default(T)
, if your return type is T
and you don't want to assume that it's nullable.
它在使用泛型时特别有用;您可能想要 return default(T)
,如果您的返回类型是T
并且您不想假设它可以为空。
回答by Graviton
The default value for class
is a null
其缺省值class
是null
回答by Darin Dimitrov
If it is a reference type, the default value will be null
, if it is a value type, then it depends.
如果是引用类型,则默认值为null
,如果是值类型,则视情况而定。
回答by Bryan Watts
Assert.IsTrue(default(MyClass) == null);
回答by Peter
Note: A DefaultValueAttribute will not cause a member to be automatically initialized with the attribute's value. You must set the initial value in your code.
注意: DefaultValueAttribute 不会导致成员使用属性值自动初始化。您必须在代码中设置初始值。
You can decorate your properties with the DefaultValueAttribute.
您可以使用 DefaultValueAttribute 装饰您的属性。
private bool myVal=false;
[DefaultValue(false)]
public bool MyProperty {
get {
return myVal;
}
set {
myVal=value;
}
}
I know this doesn't answer your question, just wanted to add this as relevant information.
我知道这不能回答您的问题,只是想将其添加为相关信息。
For more info see http://msdn.microsoft.com/en-us/library/system.componentmodel.defaultvalueattribute.aspx
有关详细信息,请参阅http://msdn.microsoft.com/en-us/library/system.componentmodel.defaultvalueattribute.aspx
回答by Konamiman
The default value for classes is null
. For structures, the default value is the same as you get when you instantiate the default parameterless constructor of the structure (which can't be overriden by the way). The same rule is applied recursively to all the fields contained inside the class or structure.
类的默认值为null
。对于结构,默认值与实例化结构的默认无参数构造函数时获得的相同(顺便说一下,它不能被覆盖)。相同的规则递归地应用于类或结构中包含的所有字段。
回答by AndyClaw
If you are looking to somehow define a non-null default value for a reference type, realize that it wouldn't make sense for a reference type to have default value. At some point a reference would need to point to the allocated memory for the constructed object. To make it possible, I imagine two implementations:
如果您希望以某种方式为引用类型定义非空默认值,请意识到引用类型具有默认值是没有意义的。在某些时候,引用需要指向为构造对象分配的内存。为了使其成为可能,我设想了两种实现:
Define a static instance as the default: this could work but would probably require a really clunky way to somehow identify to the compiler where to get the static instance. Not really a feature worth putting into the language.
Allow calling the default constructor every time the default value occurred in an expression: this would be terrible because now you have to worry about two default values possibly being unequal because you didn't override Equals to not compare references, and also terrible because of the memory management and memory allocation performance hit, and side affects from the constructor.
将静态实例定义为默认值:这可以工作,但可能需要一种非常笨拙的方式来以某种方式向编译器标识从何处获取静态实例。并不是真正值得放入该语言的功能。
允许在表达式中每次出现默认值时调用默认构造函数:这会很糟糕,因为现在你必须担心两个默认值可能不相等,因为你没有覆盖 Equals 来不比较引用,而且也很糟糕,因为内存管理和内存分配性能受到影响,并且来自构造函数的副作用。
So in summary I think it is a language feature that we really don't want. And default(MyClass) will always == null.
总而言之,我认为这是我们真正不想要的语言功能。而且 default(MyClass) 总是 == null。
回答by Yom S.
I would make this "default" class instance a field rather than property, like how System.String.Empty
looks:
我会让这个“默认”类实例成为一个字段而不是属性,就像System.String.Empty
看起来一样:
public class Person
{
public string Name { get; set; }
public string Address { get; set; }
public static readonly Person Default = new Person()
{
Name = "Some Name",
Address = "Some Address"
};
}
...
public static void Main(string[] args)
{
string address = String.Empty;
Person person = Person.Default;
//the rest of your code
}