我认为C#迫使我们编写更多(冗余)代码 [英] I think C# is forcing us to write more (redundant) code

查看:53
本文介绍了我认为C#迫使我们编写更多(冗余)代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为C#迫使我们通过强制执行一个规则来编写更多代码,这个规则可以概括为''本地变量必须在显示*之前明确* *

它的价值。''


如果您对我的意思感兴趣,请查看我的反馈意见:

http://lab.msdn.microsoft.com /produc...2-d266472a84ac

如果您认为我是对的,请投票支持此反馈。


谢谢。

I think C# is forcing us to write more code by enforcing a rule that can be
summarized as ''A local variable must be assgined *explicitly* before reading
its value.''

If you are interested in what I mean, please look at this feedback my me:

http://lab.msdn.microsoft.com/produc...2-d266472a84ac

If you think I am right, please vote for this feedback.

Thanks.

推荐答案

Neo The One写道:
Neo The One wrote:
我认为C#迫使我们编写更多代码强制执行一条可以概括为的规则在阅读其价值之前,必须明确*明确*本地变量。''

如果您对我的意思感兴趣,请看看这个反馈我的我:

http://lab.msdn.microsoft.com/produc...2-d266472a84ac

如果您认为我是对的,请投票支持此反馈。

谢谢。
I think C# is forcing us to write more code by enforcing a rule that can be
summarized as ''A local variable must be assgined *explicitly* before reading
its value.''

If you are interested in what I mean, please look at this feedback my me:

http://lab.msdn.microsoft.com/produc...2-d266472a84ac

If you think I am right, please vote for this feedback.

Thanks.




因为本地变量不能保证初始化为

任何特定的,移动这个一个警告将保证有很多项目会发生奇怪的崩溃。


因为.NET内置了机制来确保数据永远有效,

并且永远不会出现矛盾或损坏(因此需要初始化本地

变量),警告永远不够,因为警告可以被忽略,

带来不可预见的后果。


如果你开始处理随机内存对垃圾收集器的影响

作为.NET对象的地址可能是麦原因。

所以是的,我投了票,但我投票了1.


如果你想改变.NET编译器,你应该添加一个建议

代替所有局部变量。



Since the local variables are not guaranteed to be initialized to
anything specific, moving this to a warning will guarantee that there''s
a lot of projects that will have strange crashes.

Since .NET has mechanisms built in to make sure data is always valid,
and never inconsistent or corrupted (hence the need to initialize local
variables), a warning will never be enough as a warning can be ignored,
with unforseen consequences.

The effect on the garbage collector if you start to treat random memory
adresses as .NET objects is probably the main reason for this.

So yes, I voted, but I voted 1.

If you want a change to the .NET compilers, you should add a suggestion
that all local variables are zeroed out instead.


我认为我的英语不够好解释我的意思找到了。


请比较附件中的C#代码和VB代码及其IL代码

文件。您将看到VB安全地将未初始化(未明确分配的b $ b)字符串变量与Nothing进行比较。我不认为这会导致崩溃

这就是VB语言指定局部变量有默认值

值:

< quote from =" ms-help:// MS.NETFrameworkSDKv1.1/vblr7net/html/vastmDim.htm">

如果未指定初始化值对于变量,Visual Basic

将其初始化为其数据类型的默认值。默认的

初始化值如下:


0表示所有数字类型(包括字节)。

二进制0表示Char 。

所有引用类型都没有(包括Object,String和所有数组)。

布尔值为假。

00:00 AM第1年1月1日为日期。

结构或数组的每个元素都被初始化,就好像它是一个单独的

变量

< ; / quote>


也就是说VB编译器保证所有变量都有默认的
值。但是从IL代码中,我看不到s的明确赋值。到

没什么。这证明了底层的公共对象运行时(COR)有一个

机制,让所有局部变量(和其他变量)具有默认值

值。


如果以上事实都是真的,那么C#编译器正在生成REDUNDANT代码...


希望我的理解清楚。

Lasse V?¥gs?| karlsen"写道:
I think my English is not good enough to explain what I have found.

Please compare the C# code and VB code and their IL code in the attachment
file. You will see that VB safely compares an unintialized (not explicitly
assigned) string variable with Nothing. I don''t think this will cause crashes
and that''s what the VB language specifies that local variables have default
values:

<quote from="ms-help://MS.NETFrameworkSDKv1.1/vblr7net/html/vastmDim.htm">
If you do not specify an initialization value for a variable, Visual Basic
initializes it to the default value for its data type. The default
initialization values are as follows:

0 for all numeric types (including Byte).
Binary 0 for Char.
Nothing for all reference types (including Object, String, and all arrays).
False for Boolean.
12:00 AM of January 1 of the year 1 for Date.
Each element of a structure or array is initialized as if it were a separate
variable
</quote>

That is to say VB compiler guarantees that all variables have default
values. But from the IL code, I cannot see an explicit assignment of "s" to
Nothing. That proves that the underlying Common Object Runtime (COR) has a
mechanism to have all local variables (and other variables) have default
values.

If the above facts are true, then C# compiler is generating REDUNDANT code...

Hope I have made my understandings clear.
"Lasse V?¥gs?|ther Karlsen" wrote:
Neo The One写道:
Neo The One wrote:
我认为C#强制我们通过强制执行可以概括为的规则来编写更多代码''在阅读其价值之前,必须明确*明确*局部变量。''

如果您对我的意思感兴趣,请查看我的反馈意见:

http://lab.msdn.microsoft.com/produc...2-d266472a84ac

如果您认为我是对的,请投票支持此反馈。
<谢谢。
I think C# is forcing us to write more code by enforcing a rule that can be
summarized as ''A local variable must be assgined *explicitly* before reading
its value.''

If you are interested in what I mean, please look at this feedback my me:

http://lab.msdn.microsoft.com/produc...2-d266472a84ac

If you think I am right, please vote for this feedback.

Thanks.



由于不保证局部变量被初始化为
任何特定的变量,将其移动到警告将保证有
很多项目会发生奇怪的崩溃。

由于.NET内置了机制以确保数据始终有效,
从来没有不一致或损坏(因此需要初始化本地
变量),警告永远不够,因为警告可以被忽略,具有不可预见的后果。

效果在垃圾收集器上,如果你开始将随机内存处理为.NET对象可能是主要原因。
所以是的,我投了票,但我投了1票。

如果你想更改.NET编译器,你应该添加一个建议
所有局部变量都被清零。



Since the local variables are not guaranteed to be initialized to
anything specific, moving this to a warning will guarantee that there''s
a lot of projects that will have strange crashes.

Since .NET has mechanisms built in to make sure data is always valid,
and never inconsistent or corrupted (hence the need to initialize local
variables), a warning will never be enough as a warning can be ignored,
with unforseen consequences.

The effect on the garbage collector if you start to treat random memory
adresses as .NET objects is probably the main reason for this.

So yes, I voted, but I voted 1.

If you want a change to the .NET compilers, you should add a suggestion
that all local variables are zeroed out instead.



对于VB和C#,变量的创建和赋值基本相同。

为值类型分配其正常的默认值。例如,你不要
必须明确写:


int x = new int(12);


如果你只是使用int x;然后x包含零。


框架也指定了引用类型为null。


-

Bob Powell [MVP]

Visual C#,System.Drawing


Ramuseco Limited .NET咨询
http://www.ramuseco.com

在Windows窗体提示中查找精彩的Windows窗体文章技巧
http://www.bobpowell.net/tipstricks.htm


用GDI + FAQ回答那些GDI +问题
http://www.bobpowell.net/faqmain.htm


所有新文章都提供C#和VB.NET中的代码。

订阅提供的RSS提要,绝不会错过任何新文章。


Neo The One <氖******* @ discussions.microsoft.com>在消息中写道

news:94 ********************************** @ microsof t.com ...
Creation and assignment of variables is essentially the same for VB and C#.
Value types are assigned their normal default values. For example you don''t
have to explicitly write:

int x = new int(12);

and if you just use "int x;" then x contains zero.

Reference types are assigned null by the framework too.

--
Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consulting
http://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.

"Neo The One" <Ne*******@discussions.microsoft.com> wrote in message
news:94**********************************@microsof t.com...
我认为我的英语不足以解释我找到的内容。

请比较C#代码和VB代码及其IL代码附件
文件。您将看到VB安全地将未初始化(未明确分配)的字符串变量与Nothing进行比较。我不认为这会导致崩溃
这就是VB语言指定的局部变量具有默认值
< quote from =" ms-help:// MS.NETFrameworkSDKv1.1/vblr7net/html/vastmDim.htm">
如果没有为变量指定初始化值,Visual Basic
将其初始化为其数据类型的默认值。默认的
初始化值如下:

0表示所有数字类型(包括字节)。
二进制0表示Char。
所有引用类型都没有(包括对象,字符串和所有
数组)。
布尔值为假。
1年1月1日凌晨1点为日期。
结构或数组的每个元素初始化就好像它是一个单独的
变量
< / quote>

这就是说VB编译器保证所有变量都有默认值
值。但是从IL代码中,我看不到明确地将s分配给
Nothing。这证明了底层的公共对象运行时(COR)具有使所有局部变量(和其他变量)具有默认值的机制。

如果以上事实都属实,那么C#编译器正在生成REDUNDANT
代码...

希望我已经明白了我的理解。

Lasse V?gs? 卡尔森写道:
I think my English is not good enough to explain what I have found.

Please compare the C# code and VB code and their IL code in the attachment
file. You will see that VB safely compares an unintialized (not explicitly
assigned) string variable with Nothing. I don''t think this will cause
crashes
and that''s what the VB language specifies that local variables have
default
values:

<quote from="ms-help://MS.NETFrameworkSDKv1.1/vblr7net/html/vastmDim.htm">
If you do not specify an initialization value for a variable, Visual Basic
initializes it to the default value for its data type. The default
initialization values are as follows:

0 for all numeric types (including Byte).
Binary 0 for Char.
Nothing for all reference types (including Object, String, and all
arrays).
False for Boolean.
12:00 AM of January 1 of the year 1 for Date.
Each element of a structure or array is initialized as if it were a
separate
variable
</quote>

That is to say VB compiler guarantees that all variables have default
values. But from the IL code, I cannot see an explicit assignment of "s"
to
Nothing. That proves that the underlying Common Object Runtime (COR) has
a
mechanism to have all local variables (and other variables) have default
values.

If the above facts are true, then C# compiler is generating REDUNDANT
code...

Hope I have made my understandings clear.
"Lasse V?gs?ther Karlsen" wrote:
Neo The One写道:
Neo The One wrote:
>我认为C#强制我们通过强制执行
>规则来编写更多代码。可以
>总结为''在
>之前必须明确*明确*局部变量。阅读
>它的价值。''
>
>如果您对我的意思感兴趣,请查看此反馈我的
>我:
>
> http:// lab.msdn.microsoft.com/produc...2-d266472a84ac
>
>如果您认为我是对的,请投票支持此反馈。
>
>谢谢。
>
> I think C# is forcing us to write more code by enforcing a rule that
> can be
> summarized as ''A local variable must be assgined *explicitly* before
> reading
> its value.''
>
> If you are interested in what I mean, please look at this feedback my
> me:
>
> http://lab.msdn.microsoft.com/produc...2-d266472a84ac
>
> If you think I am right, please vote for this feedback.
>
> Thanks.
>



由于无法保证局部变量被初始化为具体的任何内容,因此将其移至警告将保证存在
很多项目会有奇怪的崩溃。

由于.NET内置了机制以确保数据始终有效,并且从不会出现不一致或损坏(因此需要初始化本地
变量),警告永远不够,因为警告可以忽略,具有不可预见的后果。

如果你开始治疗对垃圾收集器的影响随机记忆
作为.NET对象的地址可能是导致这种情况发生的主要原因。
所以是的,我投了票,但我投了票。

如果你想要一个更改为.NET编译器,您应该添加一个建议
所有局部变量都被清零。



Since the local variables are not guaranteed to be initialized to
anything specific, moving this to a warning will guarantee that there''s
a lot of projects that will have strange crashes.

Since .NET has mechanisms built in to make sure data is always valid,
and never inconsistent or corrupted (hence the need to initialize local
variables), a warning will never be enough as a warning can be ignored,
with unforseen consequences.

The effect on the garbage collector if you start to treat random memory
adresses as .NET objects is probably the main reason for this.

So yes, I voted, but I voted 1.

If you want a change to the .NET compilers, you should add a suggestion
that all local variables are zeroed out instead.



这篇关于我认为C#迫使我们编写更多(冗余)代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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