我有一个XAML工作流程,相当于
Bar bar;
if (foo.SomeFlag)
{
// ... do stuff
bar = .. // set bar, guaranteed unless exception thrown earlier in this block
}
// do other stuff
// here it is guaranteed that foo.SomeFlag is the same as it was before
if (foo.SomeFlag)
{
// use bar
}
在普通的C#中,我如何尝试重写它。问题是我遇到Use of unassigned variable 'bar' ..编译器错误。我了解错误,但是我认为我可以通过编写文字来克服错误
Bar bar;
const bool someFlag = foo.SomeFlag;
if (someFlag)
{
// ... do stuff
bar = .. // set bar, guaranteed unless exception thrown earlier in this block
}
// do other stuff
if (someFlag)
{
// use bar
}
但显然这不是有效的语法。
任何想法如何克服?
慕桂英546537
慕尼黑的夜晚无繁华
子衿沉夜
相关分类