在 C# 中为成员变量分配默认值的传统方法是什么?

发布于 2024-07-29 07:49:23 字数 367 浏览 1 评论 0原文

C# 中哪个更常规?

class Foo
{
  private string _first;
  private string _second;

  public Foo(string first)
  {
    _first = first;
    _second = string.Empty;
  }
}

或者

class Foo
{
  private string _first;
  private string _second = string.Empty;

  public Foo(string first)
  {
    _first = first;
  }
}

Which is more conventional in C#?

class Foo
{
  private string _first;
  private string _second;

  public Foo(string first)
  {
    _first = first;
    _second = string.Empty;
  }
}

or

class Foo
{
  private string _first;
  private string _second = string.Empty;

  public Foo(string first)
  {
    _first = first;
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

甲如呢乙后呢 2024-08-05 07:49:24

不了解约定,但更安全的方法是像第二个示例中那样初始化成员。 您可能有多个构造函数,但忘记在其中一个构造函数中执行 init。

Don't know about convention, but the safer way is initializing members as in your second example. You may have multiple constructors and forget to do the init in one of them.

云归处 2024-08-05 07:49:24

从技术上讲,当从基类的构造函数调用虚函数时,代码片段之间的行为存在细微差别,但在构造期间进行虚函数调用无论如何都是不好的做法,所以让我们忽略这一点。

我更喜欢第二个。 如果您有重载的构造函数,它可以节省一些复制/粘贴,并且没有人必须记住编写赋值语句 - 编译器会处理所有这些事情。

如果分配值的计算太复杂或者无法分配给字段初始值设定项,我将有一个受保护的(或私有)默认构造函数。

class Foo
{
    private Foo() { _second = SomeComplexCalculation(); }
    public Foo(string first) : this()
    {
       _first = first;
    }
}

如果分配的顺序很重要,那么我将有一个私有函数进行初始化。

Technically, there is a small variation in behavior between your snippets when calling virtual functions from the base class's constructor, but making virtual function calls during construction is bad practice anyway, so let's ignore that.

I'd prefer the second. If you have overloaded constructors, it saves some copy/pasting, and no one has to remember to write the assignment statement - the compiler takes care of all that.

If calculation of the assigned value is too complex or is cannot be assigned to a field initializer, I'd have a protected (or private) default constructor

class Foo
{
    private Foo() { _second = SomeComplexCalculation(); }
    public Foo(string first) : this()
    {
       _first = first;
    }
}

If the order of assignment matters, then I'll have a private function do the initialization.

梦行七里 2024-08-05 07:49:24

我喜欢第二种方式。 它看起来更自然。

I like the second way. It looks more natural.

水染的天色ゝ 2024-08-05 07:49:24

对于原始类型(String、int 等),我会选择第二种方式。

对于复杂类型,我更喜欢在构造函数内部进行初始化。

I would choose the second way for primitive types (String,int, etc.).

For complex types I prefer the intitialization inside the constuctor.

浮生未歇 2024-08-05 07:49:24

第二种方法有一些优点:首先它更容易阅读,其次它更干燥,当你有多个构造函数时,你不必手动链接它们或重复初始化,这给我们带来了第三个优点 - 它是不易出错。

The second way has few advantages: first of all it's easier to read, second it's more DRY, when you have more than one constructor you don't have to manually chain them or duplicate the initialization, which brings us to the third advantage - it's less error prone.

深府石板幽径 2024-08-05 07:49:24

我通常使用构造函数中作为构造函数参数传递的值来初始化变量(当然),并通过在成员变量的定义点直接分配相应的值来进行默认初始化(就像您在第二个解决方案中所做的那样

private string _second = string.Empty;

)属性我有一个如下所示的构造:

 public class Foo
 {

    public Foo(...)
    {
      ...
    }

    private string _Second = string.Emtpy;
    public string Second
    {
      get
      {
        return _Second;
      }
      set
      {
        _Second = value;
      }
    }

 }

I usually do the initialization of variables with values passed as constructor parameters in the constructor (of course) and default initializations by directly assigning the corresponding value at the point of the definition of the member variable (as you did in your 2nd solution)

private string _second = string.Empty;

Together with properties I have a construct like the following:

 public class Foo
 {

    public Foo(...)
    {
      ...
    }

    private string _Second = string.Emtpy;
    public string Second
    {
      get
      {
        return _Second;
      }
      set
      {
        _Second = value;
      }
    }

 }
与往事干杯 2024-08-05 07:49:24

我想说你给出的第二个例子通常是首选方式,因为它可以节省查看任何构造函数的时间,并且所有字段都在一个地方初始化(从开发人员的角度来看)。

但是,当使用第二种样式时,C# 编译器会为类中存在的每个构造函数中的每个字段生成相同的初始化值。

The second example you gave I would say is generally the preferred way because it saves time looking through any constructors and all the fields are initialized in a single place (from a developer's point of view).

But, when using this second style, the C# compiler generates the same initialization values of each field in every constructor present within the class.

凉世弥音 2024-08-05 07:49:24

Microsoft 最佳实践是在声明成员时对其进行初始化。 所以:

class Foo
{
  private string _first;
  private string _second = string.Empty;

  public Foo(string first)
  {
    _first = first;
  }
}

这就是答案。

它还告诉您不要使用默认值初始化成员。

Microsoft best practices is to initialize members while you declare them. So:

class Foo
{
  private string _first;
  private string _second = string.Empty;

  public Foo(string first)
  {
    _first = first;
  }
}

Would be the answer here.

It tells you also not to initialize members with default values.

花海 2024-08-05 07:49:24

我正在使用默认关键字

// local variable
var x = default(Action);

// member variable
this.FieldX = default(Action);

更新:添加了msdn的链接

I am using the default keyword.

// local variable
var x = default(Action);

// member variable
this.FieldX = default(Action);

Update: added the link to msdn

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文