gpt4 book ai didi

c# - 声明一个 var 而不初始化它......只是还没有

转载 作者:太空宇宙 更新时间:2023-11-03 17:30:47 24 4
gpt4 key购买 nike

有没有办法或技巧来做这样的事情:

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
}

或者也许是我正在尝试做的事情的替代方案?

最佳答案

这里的正确答案是放弃使用 var并正确指定 existingUsers 的类型外try...catch堵塞:

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
}

关于c# - 声明一个 var 而不初始化它......只是还没有,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28717397/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com