私有成员实例化/初始化的最佳实践是什么?

发布于 2024-07-22 04:47:26 字数 602 浏览 5 评论 0原文

可能的重复:
C# 成员变量初始化; 最佳实践?

,这有什么好处

public class RemotingEngine
{
    uint m_currentValueId;
    object m_lock;

    public RemotingEngine()
    {
        m_currentValueId = 0;
        m_lock = new object();
    }

与此相比

public class RemotingEngine
{
    uint m_currentValueId = 0;
    object m_lock = new object();

:我一直在避免使用第二个,只是因为它感觉“肮脏”。 它显然减少了打字,所以这对我很有吸引力。

Possible Duplicate:
C# member variable initialization; best practice?

Is there any benefit to this:

public class RemotingEngine
{
    uint m_currentValueId;
    object m_lock;

    public RemotingEngine()
    {
        m_currentValueId = 0;
        m_lock = new object();
    }

vs. this:

public class RemotingEngine
{
    uint m_currentValueId = 0;
    object m_lock = new object();

I have been avoiding the second one just because it feels 'dirty'. It is obviously less typing so that is appealing to me.

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

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

发布评论

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

评论(8

姜生凉生 2024-07-29 04:47:26

它可以改变继承情况。 有关对象初始化顺序的信息,请参阅此链接:
http://www.csharp411.com/c-object-initialization/

  1. 派生静态fields
  2. 派生静态构造函数
  3. 派生实例字段
  4. 基类静态字段
  5. 基类静态构造函数
  6. 基类实例字段
  7. 基类实例构造函数
  8. 派生实例构造函数

因此,如果这是派生类,则整个基对象会在派生字段初始化和构造函数运行之间初始化。

It can make a difference in an inheritance situation. See this link on the object initialization order:
http://www.csharp411.com/c-object-initialization/

  1. Derived static fields
  2. Derived static constructor
  3. Derived instance fields
  4. Base static fields
  5. Base static constructor
  6. Base instance fields
  7. Base instance constructor
  8. Derived instance constructor

So if this is a derived class, the entire base object is initialized between when your derived field is initialized and when your constructor runs.

帅哥哥的热头脑 2024-07-29 04:47:26

没有太大区别,我会保留第一个,因为它更具可读性。 通常变量是在类的顶部定义的,我可以去那里检查它们的默认值,而不是寻找构造函数并查看它是否设置它。 就编译器而言,没有什么区别,除非你有多个构造函数。

There's not a whole lot of difference, I'd keep with the first one because it's more readable. Usually variables are defined at the top of the class, and I can go there and check out their default values instead of hunting down a constructor and seeing if it sets it. As far as the compiler is concerned, there is no difference, unless you have multiple constructors.

虐人心 2024-07-29 04:47:26

由于这个原因,我总是使用第一个:初始化变量是构造函数的责任。 不同的构造函数可能会以不同的方式初始化变量。 所以,是的,你应该觉得用第二种方法很脏=)。

I always use the first one for this reason: It's the responsibility of the constructor to initialize variables. Different constructors may initialize variables differently. So yes, you should feel dirty doing it the second way =).

无远思近则忧 2024-07-29 04:47:26

我更喜欢第二种,在某些情况下它取决于您的编码标准。 但考虑一下处理顺序:

目标类字段初始化-> 基类字段初始化-> 基类构造函数 -> 目标类构造函数

如果基类或目标类在创建对象时出现异常并且字段已预初始化,则它将在终结时具有值,并可能会导致一些意外问题。

另请参阅 Bill Simser 的博客 C# 中的最佳实践和成员初始化

I prefer the second and in some cases it depends on your coding standards. But consider the processing sequence:

target class field initialization -> base class field initialization -> base class constructor -> target class constructor

if the base class or target class has an exception creating the object and the fields are preinitialized it will have a value at finalization and may cause some unexpected problems.

See also this blog by Bill Simser Best Practices and Member Initialization in C#

对于 int,你根本不必这样做,内在类型被初始化为 default() - 对于 int 来说,它是 0。

至于对象,在我看来这是一个品味问题。 我更喜欢前者。 如果有人重写我的构造函数,我希望他们调用 base() 以正确的状态构造它。

For the int, you don't have to do it at all, intrinsic types are initialized to default() - which, for int's is 0.

As for object, it's a matter of taste imo. I prefer the former. If someone is overriding my constructor, I expect them to call base() to construct it in a proper state.

抠脚大汉 2024-07-29 04:47:26

您希望避免在构造函数范围之外进行实例化,因为它显示了意图,特别是如果您要重写构造函数,则您有更多的灵活性

you want to avoid instantiation outside of scope of your constructor given that it shows intent and especially if you are overriding constructors you have a bit more flexibility

不再让梦枯萎 2024-07-29 04:47:26

就 IL 而言,它们是相同的。

编译器会将这个:

class Foo
{
    int bar = 1;
}

变成这样:

class Foo
{
    int bar;

    public Foo()
    {
        this.bar = 1;
    }
}

即使您自己添加一个构造函数,如下所示:

class Foo
{
    int bar = 1;

    public Foo(int bar)
    {
        this.bar = bar;
    }
}

编译器也会将其变成这样:

class Foo
{
    int bar;

    public Foo(int bar)
    {
        this.bar = 1;
        this.bar = bar;
    }
}

They are identical as far as the IL is concerned.

The compiler turns this:

class Foo
{
    int bar = 1;
}

into this:

class Foo
{
    int bar;

    public Foo()
    {
        this.bar = 1;
    }
}

Even if you add a constructor yourself like this:

class Foo
{
    int bar = 1;

    public Foo(int bar)
    {
        this.bar = bar;
    }
}

The compiler turns it into this:

class Foo
{
    int bar;

    public Foo(int bar)
    {
        this.bar = 1;
        this.bar = bar;
    }
}
罪歌 2024-07-29 04:47:26

我对此有不同的看法。 我认为你应该抽象属性并在构造函数中设置它们。 使用自动属性基本上可以消除这个问题,因为您无法初始化它们(默认值以外的任何值)。

public class RemotingEngine {
    private uint CurrentValueID { get; set; }
    private object Lock { get; set; }

    public RemotingEngine()
    {
        this.CurrentValueID = 0; // not really necessary...
        this.Lock = new object();
    }
}

I have a different take on this. I think you should abstract to properties and set them in the constructor. Using automatic properties would basically eliminate the question since you aren't able to initialize them (to anything other than the default).

public class RemotingEngine {
    private uint CurrentValueID { get; set; }
    private object Lock { get; set; }

    public RemotingEngine()
    {
        this.CurrentValueID = 0; // not really necessary...
        this.Lock = new object();
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文