关于C#的最佳编码风格 [英] About best coding Style of C#

查看:67
本文介绍了关于C#的最佳编码风格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

1.

int [] intArr = new int [255];


2.

int [] intArr = new int [255];

rgnNumberArr.Initialize();


3.

int [] intArr = new int [255];

for(int i = 0; i< intArr .lenght; i ++)

{

intArr [i] = 0 ;

}


intArr总是以0值启动?

或者我应该初始化它吗?

选择最佳代码1,2,3?

1.
int[] intArr = new int[255];

2.
int[] intArr = new int[255];
rgnNumberArr.Initialize();

3.
int[] intArr = new int[255];
for( int i=0; i<intArr .lenght; i++)
{
intArr [i] = 0;
}

intArr allways initilized with 0 value?
or should I initialize it?

choose best code 1,2,3?

推荐答案

kiplring ha scritto:
kiplring ha scritto:
1.
int [] intArr = new int [255];

2.
int [] intArr = new int [255];
rgnNumberArr.Initialize ();

3.
int [] intArr = new int [255];
for(int i = 0; i< intArr .lenght; i ++)
{
intArr [i] = 0;
}
intArr总是用0值启动?
或者我应该初始化它吗?
选择最佳代码1,2,3?
1.
int[] intArr = new int[255];

2.
int[] intArr = new int[255];
rgnNumberArr.Initialize();

3.
int[] intArr = new int[255];
for( int i=0; i<intArr .lenght; i++)
{
intArr [i] = 0;
}

intArr allways initilized with 0 value?
or should I initialize it?

choose best code 1,2,3?




嗯......我不是你的意思可以说是一个好的开发者 :-)但我应该这样做

这个:


int intArrayLength = 255;

int [] intArr = new int [255];

for(int i = 0; i< intArr .lenght; i ++)

{

intArr [i] = 0 ;

}

再见,

朱利奥 - 意大利



uhm... I''m not what you can say a "good developer" :-) but I should do like
this:

int intArrayLength = 255;
int[] intArr = new int[255];
for( int i=0; i<intArr .lenght; i++)
{
intArr [i] = 0;
}
bye,
Giulio - Italia


我是不是好的开发者或者,但我会投票给3号。


请注意,MSDN文档明确声明如果值类型

没有一个默认的构造函数,不修改数组。所以no.2是

不比no.1好。


顺便说一句,我不记得我已经读过.NET框架会将你的b / b
interger数组预先归零,但是你永远不要依赖这个

的行为......我不是对此确定无疑...


" kiplring" < ki ******** @ hotmail.com>

???????:11 ***************** *****@g10g2000cwb.googl egroups.com ...
I''m not a "good developer" either, but I''ll vote for no.3.

Note that the MSDN documentation explicitly stated that "if the value type
does not have a default constructor, the Array is not modified.", so no.2 is
no better than no.1.

btw, I somehow remember I''ve read that the .NET framework will preinitialize
interger array to zero for you but you should never rely on this
behaviour... I''m not so sure about this...

"kiplring" <ki********@hotmail.com>
???????:11**********************@g10g2000cwb.googl egroups.com...
1.
int [] intArr = new int [255];

2。
int [] intArr = new int [255];
rgnNumberArr.Initialize();

3.
int [] intArr = new int [255];
for(int i = 0; i< intArr .lenght; i ++)
{
intArr [i] = 0;
}

intArr总是初始化为0值?
或者我应该初始化它?

选择最佳代码1,2,3?
1.
int[] intArr = new int[255];

2.
int[] intArr = new int[255];
rgnNumberArr.Initialize();

3.
int[] intArr = new int[255];
for( int i=0; i<intArr .lenght; i++)
{
intArr [i] = 0;
}

intArr allways initilized with 0 value?
or should I initialize it?

choose best code 1,2,3?



" kiplring" <き******** @ hotmail.com>写道:
"kiplring" <ki********@hotmail.com> wrote:
1.
int [] intArr = new int [255];


所有带有''new''的动态分配初始化为'0'表示该类型的
。对于这个数组,这意味着整个数组将被清零

out。

2.
int [] intArr = new int [255];
rgnNumberArr.Initialize();


我不知道rgnNumberArr与intArr有什么关系,所以我不能对这段代码发表评论




Array.Initialize()用于具有构造函数的值类型(即来自

C ++);您通常不需要在C#应用程序中调用它。另外,对于C ++的人们来说,他们已经在.NET上保证默认

构建价值类型,所以现在它的必要性更低了。

3.
int [] intArr = new int [255];
for(int i = 0; i< intArr .lenght; i ++)
{
intArr [i] = 0;
}
1.
int[] intArr = new int[255];
All dynamic allocation with ''new'' is initialized to whatever ''0'' means
for that type. For this array, that means the whole array will be zeroed
out.
2.
int[] intArr = new int[255];
rgnNumberArr.Initialize();
I don''t know what rgnNumberArr has to do with intArr, so I can''t comment
on this code.

Array.Initialize() is for value types that have constructors (i.e. from
C++); you typically don''t need to call it in a C# application. Also, the
C++ folks have been toning down their guarantees on .NET about default
construction of value types, so now it''s even less necessary.
3.
int[] intArr = new int[255];
for( int i=0; i<intArr .lenght; i++)
{
intArr [i] = 0;
}




这个循环是多余的。


- Barry


-
http://barrkel.blogspot .com /


这篇关于关于C#的最佳编码风格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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