使用未分配的局部变量? [英] Use of unassigned local variable?

查看:81
本文介绍了使用未分配的局部变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的例子中,c#2.0编译器说在分配之前使用了a3和ret



据我所见,明确赋值。


如果我最后添加




{

ret = true;

a3 =" b3";

}


编译好。


------示例-------

使用System;

使用System.Collections.Generic;

使用System .Text;


名称空间范围

{

班级计划

{

static void Main(string [] args)

{

string test;

试试(out test);

}


private static bool试试(输出字符串你好)

{

bool ret;

string a1,a2,a3;


尝试

{

for(int i = 0; i< ; 3; i ++)

{

a1 =" 1" ;;

a2 = i.ToString()+ a1;

a3 =" HELLO";

}


ret = true;

}

catch(例外)

{

a3 =" sa";

}


Hello = a3;


返回;

}


}

}

In the following example, c# 2.0 compiler says that a3 and ret are used
before assigned.
as far as I can see, definite assignment is made.

If I add

finally
{
ret = true;
a3 = "b3";
}

it compiles OK.

------ example -------
using System;
using System.Collections.Generic;
using System.Text;

namespace Scope
{
class Program
{
static void Main(string[] args)
{
string test;
Try(out test);
}

private static bool Try(out string Hello)
{
bool ret;
string a1, a2, a3;

try
{
for (int i = 0; i < 3; i++)
{
a1 = "1";
a2 = i.ToString() + a1;
a3 = "HELLO";
}

ret = true;
}
catch (Exception)
{
a3 = "sa";
}

Hello = a3;

return ret;
}

}
}

推荐答案

因为有一个路径通过方法,其中变量ret不是
分配(顺便说一下调用方法Try()的错误形式)。


如果在第一个try {}块中引发异常,则执行转到

catch块,其中分配了a3。执行然后在捕获之后进入代码

,并且在分配之前尝试返回ret。


不能打扰寻找a3。


一个finally {}块总是被执行 - 所以'必然会修复它。你可以通过初始化你的变量来治愈它,当然,使用

初始化器或者在构造函数中。


HTH

Peter

" Laura T." < LT@NOWHERE.COMwrote in message

news:es ************** @ TK2MSFTNGP04.phx.gbl ...
Because there is a path through the method where the variable ret is not
assigned (bad form to call a method Try(), by the way).

If an exception is raised in the first try{} block, execution goes to the
catch block, where a3 is assigned. Execution then falls through to the code
after the catch and you try to return ret before it''s been assigned.

Can''t be bothered to look for a3.

A finally{} block is always executed - so that''s bound to fix it. You could
always cure it by initialising your variables, of course, either with
initialisers or in the constructor.

HTH
Peter
"Laura T." <LT@NOWHERE.COMwrote in message
news:es**************@TK2MSFTNGP04.phx.gbl...

在下面的例子中,c#2.0编译器说在分配之前使用了a3和ret



据我所知,明确分配。


如果我加上


终于

{

ret = true;

a3 =" b3";

}


编译好。


------示例-------

使用System;

使用System.Collections.Generic;

使用System.Text;


命名空间范围

{

class program

{

static void Main(string [] args)

{

string test;

Try(out test) );

}


private static bool试试(输出字符串你好)

{

bool ret;

string a1,a2,a3;


尝试

{

for(int i = 0;我< 3; i ++)

{

a1 =" 1";

a2 = i.ToString()+ a1;

a3 =" HELLO";

}


ret = true;

}

catch(例外)

{

a3 =" sa";

}


你好= a3;


返回ret;

}


}

}

In the following example, c# 2.0 compiler says that a3 and ret are used
before assigned.
as far as I can see, definite assignment is made.

If I add

finally
{
ret = true;
a3 = "b3";
}

it compiles OK.

------ example -------
using System;
using System.Collections.Generic;
using System.Text;

namespace Scope
{
class Program
{
static void Main(string[] args)
{
string test;
Try(out test);
}

private static bool Try(out string Hello)
{
bool ret;
string a1, a2, a3;

try
{
for (int i = 0; i < 3; i++)
{
a1 = "1";
a2 = i.ToString() + a1;
a3 = "HELLO";
}

ret = true;
}
catch (Exception)
{
a3 = "sa";
}

Hello = a3;

return ret;
}

}
}



谢谢彼得,


在catch块中都是ret和a3被分配。它们具有相同的流量。

只有a3显示为未分配,而不是。

我仍​​然觉得有些奇怪。如果我改变功能(是的,坏的

选择名称)到这个:


private static bool试试(输出字符串你好)

{

bool ret;

string a1,a2,a3;


试试

{

for(int i = 0; i< 3; i ++)

{

a1 =" 1";

a2 = i.ToString()+ a1;

a3 =" HELLO";

}

ret = true;

}

catch(例外)

{

ret = true;

a3 =" sa";

}


Hello = a3;


return ret = = true;

}


a3仍未分配给编译器。恕我直言,ret和a3都被分配了

definetly(它已经全部捕获),而且无论如何,因为它们都有相同的流量,我至少会期望它会对这两种情况都抱怨。好吧,

一个ret = true,但是如果i.tostring()抛出它就不会被执行。如果我将b $ b评论出来,那么编译器会说ret和a3都是未分配的。如果我

添加a3 =HELLO;在ret = true之后;我没有错误。



" Peter Bradley" < pb ****** @ uwic.ac.ukha scritto nel messaggio

新闻:%2 **************** @ TK2MSFTNGP06.phx .gbl ...
Thanks Peter,

in the catch block both ret and a3 are assigned. They have the same flow.
Only a3 is shown as unassigned, ret not.
I still think there is something strange. IF I change the function (yes, bad
choice for the name) to this:

private static bool Try(out string Hello)
{
bool ret;
string a1, a2, a3;

try
{
for (int i = 0; i < 3; i++)
{
a1 = "1";
a2 = i.ToString() + a1;
a3 = "HELLO";
}
ret = true;
}
catch (Exception)
{
ret = true;
a3 = "sa";
}

Hello = a3;

return ret ==true;
}

a3 is still unassigned to the compiler. IMHO both ret and a3 are assigned
definetly (it has catch all), and in any case, as they both have the same
flow, I''d at least expect that it would complain on both cases. Ok, there is
a ret=true, but it does not get executed if i.tostring() would throw. If I
comment it out, then compiler says that both ret and a3 are unassigned. If I
add a3="HELLO"; after ret=true; I get no errors.


"Peter Bradley" <pb******@uwic.ac.ukha scritto nel messaggio
news:%2****************@TK2MSFTNGP06.phx.gbl...

因为有一条路径通过方法,其中变量ret不是
分配(调用一个不好的形式)方法Try()顺便说一下。


如果在第一个try {}块中引发异常,执行转到

catch块,其中a3被分配。执行然后在捕获之后进入

代码,并且在分配之前尝试返回ret。


不能打扰寻找a3。


一个finally {}块总是被执行 - 所以'必然会修复它。你可以通过初始化你的变量来治愈它,当然,或者用初始化者或者在构造函数中使用



HTH


Peter


" Laura T." < LT@NOWHERE.COMwrote in message

news:es ************** @ TK2MSFTNGP04.phx.gbl ...
Because there is a path through the method where the variable ret is not
assigned (bad form to call a method Try(), by the way).

If an exception is raised in the first try{} block, execution goes to the
catch block, where a3 is assigned. Execution then falls through to the
code after the catch and you try to return ret before it''s been assigned.

Can''t be bothered to look for a3.

A finally{} block is always executed - so that''s bound to fix it. You
could always cure it by initialising your variables, of course, either
with initialisers or in the constructor.

HTH
Peter
"Laura T." <LT@NOWHERE.COMwrote in message
news:es**************@TK2MSFTNGP04.phx.gbl...

>在下面的例子中,c#2.0编译器说在分配之前使用了a3和ret
。据我所见,确定了分配。

如果我最后添加

{
ret = true;
a3 =" b3";
}
------例子-------
使用System;
使用System.Collections.Generic;
使用System.Text;

命名空间范围
{
类程序
{
静态无效Main(string [] args)
{
字符串测试;
尝试(out test);
}
私有静态bool尝试(输出字符串Hello)
{
bool ret;
字符串a1,a2,a3;

尝试
{
t i = 0;我< 3; i ++)
{
a1 =" 1" ;;
a2 = i.ToString()+ a1;
a3 =" HELLO";
} {
/> Hello = a3;

返回ret;
}

}


>In the following example, c# 2.0 compiler says that a3 and ret are used
before assigned.
as far as I can see, definite assignment is made.

If I add

finally
{
ret = true;
a3 = "b3";
}

it compiles OK.

------ example -------
using System;
using System.Collections.Generic;
using System.Text;

namespace Scope
{
class Program
{
static void Main(string[] args)
{
string test;
Try(out test);
}

private static bool Try(out string Hello)
{
bool ret;
string a1, a2, a3;

try
{
for (int i = 0; i < 3; i++)
{
a1 = "1";
a2 = i.ToString() + a1;
a3 = "HELLO";
}

ret = true;
}
catch (Exception)
{
a3 = "sa";
}

Hello = a3;

return ret;
}

}
}




嗨Laura,


的确,a3总是在你的代码中分配,也就是ret。

的区别在于你在for循环中分配a3。可能的编译器

没有看到它:)有很多像这样的情况,但在

dotNet中它不是警告,而是一个错误。在这种情况下,您可以在声明它以使编译器满意时分配

null左右。


-

rgds ,Wilfried [MapPoint MVP]
http://www.mestdagh.biz


Laura T.写道:
Hi Laura,

Indeed, a3 is always assigned in your code as well is ret. The
difference is that you assign a3 in the for loop. Possible the compiler
does not "see" it :) There are lots of situations like this, but in
dotNet it is not a warning, but an error. In this case you could assign
null or so when declaring it to make the compiler happy :)

--
rgds, Wilfried [MapPoint MVP]
http://www.mestdagh.biz

Laura T. wrote:

谢谢Peter,


in catch阻止ret和a3分配。它们具有相同的流量。

只有a3显示为未分配,而不是。

我仍​​然觉得有些奇怪。如果我改变功能(是的,坏的

选择名称)到这个:


private static bool试试(输出字符串你好)

{

bool ret;

string a1,a2,a3;


试试

{

for(int i = 0; i< 3; i ++)

{

a1 =" 1";

a2 = i.ToString()+ a1;

a3 =" HELLO";

}

ret = true;

}

catch(例外)

{

ret = true;

a3 =" sa";

}


Hello = a3;


return ret = = true;

}


a3仍未分配给编译器。恕我直言,ret和a3都被分配了

definetly(它已经全部捕获),而且无论如何,因为它们都有相同的流量,我至少会期望它会对这两种情况都抱怨。好吧,

一个ret = true,但是如果i.tostring()抛出它就不会被执行。如果我将b $ b评论出来,那么编译器会说ret和a3都是未分配的。如果我

添加a3 =HELLO;在ret = true之后;我没有错误。



" Peter Bradley" < pb ****** @ uwic.ac.ukha scritto nel messaggio

新闻:%2 **************** @ TK2MSFTNGP06.phx .gbl ...
Thanks Peter,

in the catch block both ret and a3 are assigned. They have the same flow.
Only a3 is shown as unassigned, ret not.
I still think there is something strange. IF I change the function (yes, bad
choice for the name) to this:

private static bool Try(out string Hello)
{
bool ret;
string a1, a2, a3;

try
{
for (int i = 0; i < 3; i++)
{
a1 = "1";
a2 = i.ToString() + a1;
a3 = "HELLO";
}
ret = true;
}
catch (Exception)
{
ret = true;
a3 = "sa";
}

Hello = a3;

return ret ==true;
}

a3 is still unassigned to the compiler. IMHO both ret and a3 are assigned
definetly (it has catch all), and in any case, as they both have the same
flow, I''d at least expect that it would complain on both cases. Ok, there is
a ret=true, but it does not get executed if i.tostring() would throw. If I
comment it out, then compiler says that both ret and a3 are unassigned. If I
add a3="HELLO"; after ret=true; I get no errors.


"Peter Bradley" <pb******@uwic.ac.ukha scritto nel messaggio
news:%2****************@TK2MSFTNGP06.phx.gbl...

>因为有一个路径通过方法,其中没有分配变量ret(调用方法的错误形式试试(顺便说一句)。

如果在第一个try {}块中引发异常,执行将转到
catch块,其中分配了a3。执行然后在捕获之后进入
代码,并且在分配之前尝试返回ret。

不能为寻找a3而烦恼。

始终执行finally {}块 - 这样就必须修复它。你可以通过初始化你的变量来治愈它,当然,或者使用初始化器或者在构造函数中。

HTH

Peter
" Laura T." < LT@NOWHERE.COMwrote in message
新闻:es ************** @ TK2MSFTNGP04.phx.gbl ...
>Because there is a path through the method where the variable ret is not
assigned (bad form to call a method Try(), by the way).

If an exception is raised in the first try{} block, execution goes to the
catch block, where a3 is assigned. Execution then falls through to the
code after the catch and you try to return ret before it''s been assigned.

Can''t be bothered to look for a3.

A finally{} block is always executed - so that''s bound to fix it. You
could always cure it by initialising your variables, of course, either
with initialisers or in the constructor.

HTH
Peter
"Laura T." <LT@NOWHERE.COMwrote in message
news:es**************@TK2MSFTNGP04.phx.gbl...

>>在下面的例子中,c#2.0编译器说在分配之前使用了a3和ret
。据我所见,明确赋值。

如果我最后添加

{
ret = true;
a3 =" b3";
}

它编译好。

------示例-------
使用System;
使用System.Collections.Generic;
使用System.Text;

命名空间范围
{
类程序
{
静态无效Main(string [] args)
{
字符串测试;
试试(out test);
}
私有静态bool试试(输出字符串你好)
{
bool ret;
字符串a1,a2,a3;

尝试
{
for(int i = 0;我< 3; i ++)
{
a1 =" 1" ;;
a2 = i.ToString()+ a1;
a3 =" HELLO";
} {
/> Hello = a3;

返回ret;
}

}


>>In the following example, c# 2.0 compiler says that a3 and ret are used
before assigned.
as far as I can see, definite assignment is made.

If I add

finally
{
ret = true;
a3 = "b3";
}

it compiles OK.

------ example -------
using System;
using System.Collections.Generic;
using System.Text;

namespace Scope
{
class Program
{
static void Main(string[] args)
{
string test;
Try(out test);
}

private static bool Try(out string Hello)
{
bool ret;
string a1, a2, a3;

try
{
for (int i = 0; i < 3; i++)
{
a1 = "1";
a2 = i.ToString() + a1;
a3 = "HELLO";
}

ret = true;
}
catch (Exception)
{
a3 = "sa";
}

Hello = a3;

return ret;
}

}
}



这篇关于使用未分配的局部变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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