我应该显式初始化所有成员变量吗? [英] Should I explicitly initialize all member variables?

查看:89
本文介绍了我应该显式初始化所有成员变量吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我来自Ada和C ++编程的背景,在那里它被认为是明确初始化每个变量的良好做法。


现在,我用C#编程我认为最好不要
初始化类成员,因为每种类型都有明确定义的初始化

无论如何。

例如。


class C

{


int i = 0; //冗余


int j; //更好?


MyClass m = null; //多余的


MyClass m; //更好?

}


什么是最佳做法?


TIA,

Javaman

I come from a background of Ada and C++ programming, where it was considered
good practice to explicitly initialize every variable.

Now that I''m programming in C# I think that it would be best NOT to
initialize class members, because every type has well defined initialization
anyway.

eg.

class C
{

int i = 0; // redundant

int j; // better?

MyClass m = null; // redundant

MyClass m; // better?
}

What''s the best practice?

TIA,

Javaman

推荐答案

Javaman59,


做的最好的事情是初始化每个变量。

然后,每个变量总是有一个默认值。

它应该可以让你的代码更整洁,更容易破译,它

让事情变得清晰。


对不起,如果这不是你想要的,但是,


Seen Sharp

Javaman59,

The best thing to do is to initialize every variable.
Then, there''s always a default value for every variable.
It should probably make your code ''neater'' and easier to decipher, it
makes thing clear.

Sorry if this isn''t what you like but,

Seen Sharp


我必须强烈反对。您不应该将每个变量初始化为

其默认值,因为这会增加额外的开销。变量

初始化将被移动到默认构造函数,如果你通过声明它来初始化




class myclass

{

int i = 0;

}

上面的
c#会产生一个构造函数在里面你会看到

this.i = 0;


任务。


浪费周期男人。

Visually Seen #write:
I must STRONGLY disagree. You should not initialize every variable to
its default value as that adds extra overhead. The variable
initialization is moved to the default constructor if you initialize it
by declaring it like this:

class myclass
{
int i = 0;
}

c# for above would generate a constructor and inside you would see
this.i = 0;

assignment.

Waste of cycles man.
Visually Seen # wrote:
Javaman59,

最好的办法是初始化每个变量。
然后,'' s始终是每个变量的默认值。
它可能会使你的代码整齐并且更容易破译,它会使事情变得清晰。

如果这不是你喜欢什么,但是,

Seen Sharp
Javaman59,

The best thing to do is to initialize every variable.
Then, there''s always a default value for every variable.
It should probably make your code ''neater'' and easier to decipher, it
makes thing clear.

Sorry if this isn''t what you like but,

Seen Sharp



我想说不要初始化它们,因为它增加了额外的代码底层

MSIL最终需要更长时间才能执行。

Brian Delahunty

爱尔兰

http://briandela.com/博客

" Javaman59"写道:
I would say not to initialize them as it adds extra code to the underlying
MSIL which in the end takes longer to execute.
Brian Delahunty
Ireland

http://briandela.com/blog
"Javaman59" wrote:
我来自Ada和C ++编程的背景,它被认为是明确初始化每个变量的良好实践。

现在我正在使用C#进行编程我认为最好不要初始化类成员,因为无论如何,每种类型都有明确定义的初始化。

例如。

C班
{i />
int i = 0; //冗余

int j; //更好?

MyClass m = null; //多余的

MyClass m; //更好?
}

什么是最佳做法?

TIA,

Javaman
I come from a background of Ada and C++ programming, where it was considered
good practice to explicitly initialize every variable.

Now that I''m programming in C# I think that it would be best NOT to
initialize class members, because every type has well defined initialization
anyway.

eg.

class C
{

int i = 0; // redundant

int j; // better?

MyClass m = null; // redundant

MyClass m; // better?
}

What''s the best practice?

TIA,

Javaman



这篇关于我应该显式初始化所有成员变量吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆