这应该是一个警告,至少? [英] Should this be a warning, at least?

查看:71
本文介绍了这应该是一个警告,至少?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C#编译器提供有关未使用变量的警告。我知道它需要一个关于未分配引用的

警告。例如,这可以在没有

的情况下编译任何警告/错误,但当然在运行时会爆炸:


SqlConnection cnn = null;

试试

{

新的SqlConnection(myConnectionString);

cnn.Open()

评论?


Rich Sienkiewicz

Nice Systems,Inc。

解决方案



Rich Sienkiewicz写道:

C#编译器提供有关未使用变量的警告。我瘦它需要一个关于未分配引用的警告。例如,这没有任何警告/错误编译,但运行时当然会爆炸:

SqlConnection cnn = null;
尝试
{
新的SqlConnection(myConnectionString);
cnn.Open()

评论?




我会说不。很难弄清楚cnn是否可以被设置为

的副作用

其他一些动作。是的,这个特别的一个很容易被发现,

但是cnn

被分配给某个东西(null)。如果您刚刚写道:


SqlConnection cnn;


cnn.Open();


我预计会出现错误或至少发出警告。


Matt


Rich,


通过设置* null *来初始化cnn。如果你不这样做,编译器将会b / b
抱怨你正在尝试使用单位变量。

-


Stoitcho Goutsev(100)

" Rich Sienkiewicz" <日************* @ discussions.microsoft.com>写在

消息新闻:26 ********************************** @ microsof t.com ...

C#编译器会发出有关未使用变量的警告。我瘦它需要一个关于未分配引用的警告。例如,这没有任何警告/错误编译,但运行时当然会爆炸:

SqlConnection cnn = null;
尝试
{
新的SqlConnection(myConnectionString);
cnn.Open()

评论?

Rich Sienkiewicz
Nice Systems,Inc。



Rich,


我同意,对于这种情况,它会很好,但它会非常好

limited,仅受堆栈上声明的变量限制。如果它们是b / b
参数或字段,则需要进行更加密集的

静态分析。


希望这个帮助。


-

- Nicholas Paldino [.NET / C#MVP]

- mv*@spam.guard.caspershouse.com

" Rich Sienkiewicz" <日************* @ discussions.microsoft.com>写在

消息新闻:26 ********************************** @ microsof t.com ...

C#编译器会发出有关未使用变量的警告。我瘦它需要一个关于未分配引用的警告。例如,这没有任何警告/错误编译,但运行时当然会爆炸:

SqlConnection cnn = null;
尝试
{
新的SqlConnection(myConnectionString);
cnn.Open()

评论?

Rich Sienkiewicz
Nice Systems,Inc。



The C# compiler gives warnings about unused variables. I thin it needs a
warning about an unassigned reference. For instance, this compiles without
any warnings/errors, but of course blows up when run:

SqlConnection cnn = null;
try
{
new SqlConnection(myConnectionString);
cnn.Open()
Comments?

Rich Sienkiewicz
Nice Systems, Inc.

解决方案


Rich Sienkiewicz wrote:

The C# compiler gives warnings about unused variables. I thin it needs a
warning about an unassigned reference. For instance, this compiles without
any warnings/errors, but of course blows up when run:

SqlConnection cnn = null;
try
{
new SqlConnection(myConnectionString);
cnn.Open()
Comments?



I''d say no. Its too hard to figure out if cnn could have been set as a
side-effect
of some other action. Yes, this particular one would be easy to spot,
but cnn
IS assigned to something (null). If you just wrote:

SqlConnection cnn;

cnn.Open();

I''d expect an error, or a warning at the very least.

Matt


Rich,

You initialize cnn by setting *null*. If you don''t do that the compiler will
complain that you are trying to use unitialized variable.
--

Stoitcho Goutsev (100)

"Rich Sienkiewicz" <Ri*************@discussions.microsoft.com> wrote in
message news:26**********************************@microsof t.com...

The C# compiler gives warnings about unused variables. I thin it needs a
warning about an unassigned reference. For instance, this compiles without
any warnings/errors, but of course blows up when run:

SqlConnection cnn = null;
try
{
new SqlConnection(myConnectionString);
cnn.Open()
Comments?

Rich Sienkiewicz
Nice Systems, Inc.



Rich,

I agree, for this situation, it would be nice, but it would be very
limited, limited only by variables declared on the stack. If they were
parameters or fields, it would require one to do a much more intensive
static analysis.

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Rich Sienkiewicz" <Ri*************@discussions.microsoft.com> wrote in
message news:26**********************************@microsof t.com...

The C# compiler gives warnings about unused variables. I thin it needs a
warning about an unassigned reference. For instance, this compiles without
any warnings/errors, but of course blows up when run:

SqlConnection cnn = null;
try
{
new SqlConnection(myConnectionString);
cnn.Open()
Comments?

Rich Sienkiewicz
Nice Systems, Inc.



这篇关于这应该是一个警告,至少?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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