本文介绍了声明一个 var 而不初始化它......只是还没有的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有没有办法或技巧来做这样的事情:
Is there a way or a trick to do something like:
var existingUsers; // This is not possible, but i need it to be global :)
try
{
existingUsers = Repo.GetAll(); // This may crash, and needs to be in a try
}
catch (Exception)
{
throw new Exception("some error, don't bother");
}
if (existingUsers.Count > 0)
{
//some code
}
或者也许可以替代我正在尝试做的事情?
Or maybe an alternative for what I'm trying to do?
推荐答案
这里的正确答案是放弃使用 var
并正确指定 existingUsers
的类型try...catch
块之外:
The correct answer here is to drop the use of var
and to correctly specify the type of existingUsers
outside the try...catch
block:
List<User> existingUsers = null; // or whatever is the right type!
try
{
existingUsers = Repo.GetAll(); // This may crash, and needs to be in a try
}
catch (Exception)
{
throw new Exception("some error, don't bother");
}
if (existingUsers.Count > 0)
{
//some code
}
这篇关于声明一个 var 而不初始化它......只是还没有的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!