【发布时间】:2015-04-27 08:25:33
【问题描述】:
有没有办法或技巧来做类似的事情:
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
}
或者也许可以替代我正在尝试做的事情?
【问题讨论】:
-
你为什么不明确声明它的强类型呢?
-
如果你需要它是全局的,因为你在 try/catch 之外使用了它的一些方法/属性,你假设它有一些接口。将
var替换为ISomeInterface,比如ICollection可能吗? -
@VivekGupta - 您不能将无类型变量声明为
null。 -
我如何对 cmets 投反对票? @vivekGupta 看着你
-
你想完成什么?这篇文章花费了你足够多的按键来输入实际类型,而不是 var 在今年剩下的时间里。