这里是我的代码:如何声明一个空变量?
private void button1_Click(object sender, EventArgs e)
{
var api = RiotApi.GetInstance("KEY");
try
{
var game = api.GetCurrentGame(RiotSharp.Platform.EUW1, 79200188);
}
catch (RiotSharpException ex)
{
throw;
}
foreach (var player in game.Participants) // Can't find game variable
{
}
}
我不能叫game.Participants在我的foreach循环,因为我try语句里面初始化游戏。我不能在try语句之外初始化游戏,但是因为要这样做,我必须给它一个临时值,我不知道它会是什么样的价值。
有没有办法将变量声明为null?或者是否有解决此问题的其他方法?
像'游戏游戏= NULL;'?请注意,你不能放'var game = null;'并且必须声明实际的类型。 –
考虑将循环移入尝试,因为如果游戏不存在或不存在,您不想访问游戏? –
在try块内包含foreach有什么错误? –