[英]Do I need to set initial values in my class constructor?
I have the following class: 我有以下课程:
public class Detail
{
public Detail()
{
this.File = String.Empty;
this.State = false;
this.Tag1 = 0
}
public string File { get; set; }
public bool State { get; set; }
public int Tag1 { get; set; }
}
}
Do I need to set the initial values like this in the constructor or will these just default when I create the class. 我需要在构造函数中设置这样的初始值,还是在创建类时将它们设置为默认值? How about programming practice. 编程实践如何? Does it look better to set them here even if not needed? 即使不需要,在这里设置它们看起来更好吗?
State
and Tag1
will default to false
and 0
respectively, but File
will default to null
. State
和Tag1
分别默认为false
和0
,但是File
默认为null
。 If you want string.Empty
, you'll have to assign it. 如果您需要string.Empty
,则必须对其进行分配。
Member fields are initialized with default values for you. 成员字段将为您初始化为默认值。 So, no you don't have to set if the value is default ( 0
for int
, false
for bool
, null
for reference types). 因此,如果值是默认值,则不必设置( int
为0
, bool
为false
,引用类型为null
)。 However string.Empty
is not default value for string
. 但是string.Empty
不是string
默认值。 So you need to set it to string.Empty
if null doesn't work for you. 因此,您需要将其设置为string.Empty
如果null对您不起作用。
The initial value of a field, whether it be a static field or an instance field, is the default value of the field's type. 字段的初始值(无论是静态字段还是实例字段)都是该字段类型的默认值。
The default value of a variable depends on the type of the variable and is determined as follows: 变量的默认值取决于变量的类型,并确定如下:
No, you do not need to do this. 不,您不需要这样做。 All property values are automatically set to their default value when a class constructor is called. 调用类构造函数时,所有属性值都会自动设置为其默认值。 See this page for info on default values in C#. 有关C#中默认值的信息,请参见此页面 。
You might want to consider it if you would rather have a string be empty rather than null, or whether or if you want a collection (a List for example) to be initialized automatically, though. 但是,如果您希望字符串为空而不是null,或者是否想要自动初始化集合(例如List),则可能要考虑一下。 What you've done certainly isn't bad practice at all, just unnecessary. 您所做的当然绝对不是坏习惯,只是不必要的。
Clearly, in your example you can't set them in the properties themselves, because you don't have an explicit backing variable. 显然,在您的示例中,您无法在属性本身中设置它们,因为您没有显式的后备变量。
When you set default values in the constructor, it's fine, but you need to be careful to always call that constructor from others you may have, like: 当您在构造函数中设置默认值时,这很好,但是您需要注意始终从可能拥有的其他构造函数中调用该构造函数,例如:
public Foo () {
this.Happy = true;
this.BasicUnit = 92;
}
public Foo (bool happy) : this() {
this.Happy = happy;
}
It can get a little confusing as to what is what. 关于什么是东西可能会有些混乱。 So, personally, I like to set default values explicitly in the backing variables. 因此,就我个人而言,我想在支持变量中显式设置默认值。
-- Edit: I see I may have misinterpreted your question - as others have said, all member variables are given default values, local variables are not. -编辑:我看到我可能误解了您的问题-正如其他人所说的那样,所有成员变量都被赋予了默认值,而局部变量没有被赋予。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.