简体   繁体   English

我是否需要在类构造函数中设置初始值?

[英]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 . StateTag1分别默认为false0 ,但是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). 因此,如果值是默认值,则不必设置( int0boolfalse ,引用类型为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: 变量的默认值取决于变量的类型,并确定如下:

  • For a variable of a value-type, the default value is the same as the value computed by the value-type's default constructor 对于值类型的变量,默认值与值类型的默认构造函数计算的值相同
  • For a variable of a reference-type, the default value is null 对于引用类型的变量,默认值为null

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.

相关问题 你调用的对象是空的。 我需要构造函数吗? - Object reference not set to an instance of an object. Do I need a constructor? 我是否需要为抽象类提供一个受保护的空构造函数? - Do I need to provide an empty protected constructor for an abstract class? 为什么我需要在 Controller 类构造函数中和 Repository 类构造函数中启动 DBContext 对象 - Why do i need to Initiate the DBContext object inside the Controller class constructor & inside the Repository class constructor 我需要一个具有继承的构造函数吗? - Do I need a constructor with Inheritance? 如何将我的数据库设置回初始值 - How to set my database back to the initial values 您需要一个带有构造函数的只读属性,让您为该属性设置值吗? - What do you need a read-only property with a constructor that let's you set the values in that property for? 我想我需要一个带有构造函数的静态类? - I think I need a static class with a constructor? 当我可以在继承自其的类中执行相同的操作时,为什么要在类中设置构造函数以设置基本参数? - Why set up a constructor in a class to set a base parameter when I could do the same in a class it inherits from? 为什么子类没有给构造函数添加任何额外逻辑时,我需要重写父类 DbContext 的构造函数? - Why do I need to override the constructor for the parent class DbContext when the sub class doesn't add any extra logic to the constructor? 如何在派生构造函数中设置基类的值 - How to set base class the values in the derived constructor
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM