使用未分配的局部变量。但总是陷入任务

发布于 2024-12-22 10:12:28 字数 1052 浏览 4 评论 0原文

有了这段代码,我不明白为什么如果在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

屋顶上的小猫咪 2024-12-29 10:12:28

C# 编译器执行的明确赋值跟踪不一定执行完整的分析(这在一般情况下是不可能的) - 有一些规则限制编译器执行的分析的复杂程度。此处涵盖 finally 块的规则记录在 http: //msdn.microsoft.com/en-us/library/aa691181.aspx

对于以下形式的 try 语句 stmt

try try-block finally finally-block

  • try-block开头的v的明确赋值状态与vtry-block处的明确赋值状态相同
    stmt 的开头。
  • finally-block 开头的 v 的明确赋值状态与位于 v 的明确赋值状态相同
    stmt的开头。
  • ...

因此,对于您的特定示例,由于 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:

For a try statement stmt of the form:

try try-block finally finally-block

  • The definite assignment state of v at the beginning of try-block is the same as the definite assignment state of v at the
    beginning of stmt.
  • The definite assignment state of v at the beginning of finally-block is the same as the definite assignment state of v at
    the beginning of stmt.
  • ...

So for your particular example, since currency is not definitely assigned at the beginning of the try block, it is considered to be not definitely assigned at the beginning of the finally block.

内心荒芜 2024-12-29 10:12:28

如果 newCurrencyVO()catch 块中引发异常怎么办?啊哈!

What if new CurrencyVO() causes an exception in the catch block? A-ha!

梦中的蝴蝶 2024-12-29 10:12:28

您可以在不符合语言规范的情况下推理出它。这里的失败模式是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.

千と千尋 2024-12-29 10:12:28

您正在 catch 块内创建一个新的 CurrencyVO 对象:该对象仅在出现错误/异常时才会执行。因此,如果没有遇到异常:currency 变量将不会被分配。这就是为什么你不能使用它。

编辑:对要编译的代码进行以下更改:

CurrencyVO currency = null;

最后:

if (currency != null)
   model.PublishedContainer.Currency = 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: the currency variable will not get assigned. That's why you can't use it.

edit: make the following changes for your code to compile:

CurrencyVO currency = null;

and in finally:

if (currency != null)
   model.PublishedContainer.Currency = currency;

http://social.msdn.microsoft.com/forums/en-US/netfxbcl/thread/a994a0ff-432b-4d23-b7d2-838b0b961de0

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文