在 C# 中为成员变量分配默认值的传统方法是什么?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
不了解约定,但更安全的方法是像第二个示例中那样初始化成员。 您可能有多个构造函数,但忘记在其中一个构造函数中执行 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.
从技术上讲,当从基类的构造函数调用虚函数时,代码片段之间的行为存在细微差别,但在构造期间进行虚函数调用无论如何都是不好的做法,所以让我们忽略这一点。
我更喜欢第二个。 如果您有重载的构造函数,它可以节省一些复制/粘贴,并且没有人必须记住编写赋值语句 - 编译器会处理所有这些事情。
如果分配值的计算太复杂或者无法分配给字段初始值设定项,我将有一个受保护的(或私有)默认构造函数。
如果分配的顺序很重要,那么我将有一个私有函数进行初始化。
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
If the order of assignment matters, then I'll have a private function do the initialization.
我喜欢第二种方式。 它看起来更自然。
I like the second way. It looks more natural.
对于原始类型(String、int 等),我会选择第二种方式。
对于复杂类型,我更喜欢在构造函数内部进行初始化。
I would choose the second way for primitive types (String,int, etc.).
For complex types I prefer the intitialization inside the constuctor.
第二种方法有一些优点:首先它更容易阅读,其次它更干燥,当你有多个构造函数时,你不必手动链接它们或重复初始化,这给我们带来了第三个优点 - 它是不易出错。
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.
我通常使用构造函数中作为构造函数参数传递的值来初始化变量(当然),并通过在成员变量的定义点直接分配相应的值来进行默认初始化(就像您在第二个解决方案中所做的那样
)属性我有一个如下所示的构造:
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)
Together with properties I have a construct like the following:
我想说你给出的第二个例子通常是首选方式,因为它可以节省查看任何构造函数的时间,并且所有字段都在一个地方初始化(从开发人员的角度来看)。
但是,当使用第二种样式时,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.
Microsoft 最佳实践是在声明成员时对其进行初始化。 所以:
这就是答案。
它还告诉您不要使用默认值初始化成员。
Microsoft best practices is to initialize members while you declare them. So:
Would be the answer here.
It tells you also not to initialize members with default values.
我正在使用默认关键字。
更新:添加了msdn的链接
I am using the default keyword.
Update: added the link to msdn