使用未分配的局部变量。但总是陷入任务
有了这段代码,我不明白为什么如果在finally块中分配一个变量不明白它总是会被分配。我认为我缺少一个不会分配货币的有效选项。如果你知道,就会很好地理解为什么。非常感谢!
谢谢!
CurrencyVO currency;
try
{
if (idConnection.HasValue && idConnection != 0)
{
currencyConnection = client.GetConnection(idConnection.Value);
model.Connection = currencyConnection;
}
else
{
int providerUserKey = (int)Models.UserModel.GetUser().ProviderUserKey;
currencyConnection = client.GetConnection(providerUserKey);
}
currency = model.Currencies.SingleOrDefault(c => c.IdCountry == currencyConnection.idcountry) ?? new CurrencyVO();
}
catch
{
currency = new CurrencyVO();
}
finally
{
model.PublishedContainer.Currency = currency;
}
错误发生在finally块上。如果我像这样将它从finally块中取出:
} catch {
currency = new CurrencyVO();
}
model.PublishedContainer.Currency = currency;
它工作正常。
having this code, I don't understand why if assigning a variable in a finally block doesn't understand it will ALWAYS be assigned. I think I missing a valid option where currency won't be assigned. If you know, will be great to understand why. much appreciate it!
Thanks!
CurrencyVO currency;
try
{
if (idConnection.HasValue && idConnection != 0)
{
currencyConnection = client.GetConnection(idConnection.Value);
model.Connection = currencyConnection;
}
else
{
int providerUserKey = (int)Models.UserModel.GetUser().ProviderUserKey;
currencyConnection = client.GetConnection(providerUserKey);
}
currency = model.Currencies.SingleOrDefault(c => c.IdCountry == currencyConnection.idcountry) ?? new CurrencyVO();
}
catch
{
currency = new CurrencyVO();
}
finally
{
model.PublishedContainer.Currency = currency;
}
the error happens on the finally block. If i take it out of the finally block like this :
} catch {
currency = new CurrencyVO();
}
model.PublishedContainer.Currency = currency;
it works fine.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
C# 编译器执行的明确赋值跟踪不一定执行完整的分析(这在一般情况下是不可能的) - 有一些规则限制编译器执行的分析的复杂程度。此处涵盖
finally
块的规则记录在 http: //msdn.microsoft.com/en-us/library/aa691181.aspx:因此,对于您的特定示例,由于
currency
未在try
块的开头明确分配,因此它被认为在该块的开头未明确分配finally
块。The definite assignment tracking that the C# compiler performs doesn't necessarily perform a complete analysis (that wouldn't be possible in the general case) - there are rules that restrict how complex of an analysis the compiler will perform. The rule covering the
finally
block here is documented at http://msdn.microsoft.com/en-us/library/aa691181.aspx:So for your particular example, since
currency
is not definitely assigned at the beginning of thetry
block, it is considered to be not definitely assigned at the beginning of thefinally
block.如果
newCurrencyVO()
在catch
块中引发异常怎么办?啊哈!What if
new CurrencyVO()
causes an exception in thecatch
block? A-ha!您可以在不符合语言规范的情况下推理出它。这里的失败模式是catch块中的语句可以抛出异常。 C# 很可能支持在构造函数中抛出异常。但一般来说,任何语句都可能引发异常。这将使变量未初始化。 finally 块中的赋值将始终执行。在这种情况下,请使用未初始化的变量。
如果相关的话,您需要考虑在这种情况下会发生什么。但明显的解决方法是简单地将变量显式初始化为 null。
You can reason it out without hitting the language spec. The failure mode here is that the statement in the catch block can throw an exception. Quite possible, throwing an exception in a constructor is supported in C#. But true in general, any statement can throw an exception. Which will leave the variable uninitialized. The assignment in the finally block will always execute. And use an uninitialized variable in that case.
You'll need to think about what you to have happen in that case, if it is at all relevant. But the obvious workaround is to simply initialize the variable to null explicitly.
您正在 catch 块内创建一个新的
CurrencyVO
对象:该对象仅在出现错误/异常时才会执行。因此,如果没有遇到异常:currency
变量将不会被分配。这就是为什么你不能使用它。编辑:对要编译的代码进行以下更改:
最后:
http://social.msdn.microsoft.com/forums/en-US/netfxbcl/thread/a994a0ff-432b-4d23-b7d2-838b0b961de0
You are creating a new
CurrencyVO
object inside the catch block: which will only execute if there is an error/exception. Therefore, if you encounter no exception: thecurrency
variable will not get assigned. That's why you can't use it.edit: make the following changes for your code to compile:
and in finally:
http://social.msdn.microsoft.com/forums/en-US/netfxbcl/thread/a994a0ff-432b-4d23-b7d2-838b0b961de0