我正在尝试创建能够快速运行的代码.为此,我在需要时对一些变量进行了初始化.例如:(我无法更改标签\ goto部分,我必须在这种情况下使用它)
bool Func(bool BooleanParameter) {
if (BooleanParameter)
goto _true;
else
goto _false;
_true:
string str; //Some code after that one that does with this variable
return false;
_false:
return true; //Exception because str doesn't initialized
}
Run Code Online (Sandbox Code Playgroud)
但是有一个例外,因为有一种方法可以不初始化变量,并且变量最终会破坏.
为什么不呢:
bool Func(bool booleanParameter)
{
if (booleanParameter)
{
string str;
// ...
return false;
}
return true;
}
Run Code Online (Sandbox Code Playgroud)
?
这似乎达到了预期的结果,没有可疑的使用goto等.