Use of unassigned local variable. But always falls into assignment

怎甘沉沦 提交于 2019-12-10 15:27:20

问题


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.


回答1:


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.




回答2:


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




回答3:


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.




回答4:


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



来源:https://stackoverflow.com/questions/8597757/use-of-unassigned-local-variable-but-always-falls-into-assignment

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!