简体   繁体   English

如何声明一个空变量?

[英]How can I declare an empty variable?

Here's my code: 这是我的代码:

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
        {

        }
    }

I can't call game.Participants in my foreach loop because i initialize game inside the try statement. 我无法调用game.foreach循环中的参与者,因为我在try语句中初始化了游戏。 I can't initialize game outside the try statement either though because to do that i would have to give it a temporary value and I don't know what kind of value it will be. 我也不能在try语句之外初始化游戏,因为要做到这一点,我必须给它一个临时值,而我不知道它将是什么样的值。

Is there a way to declare a variable as null? 有没有办法将变量声明为null? Or is there potentially a different way to solve this? 还是有可能以其他方式解决此问题?

You should declare variable before try-catch block, otherwise it will not be visible outside the try-catch block: 你应该声明前变量try-catch块,否则它不会是外部不可见try-catch块:

TypeOfGame game = null; // declare local variable here
// note that you should provide initial value as well

try
{
   // assigne it here
   game = api.GetCurrentGame(RiotSharp.Platform.EUW1, 79200188);
}
catch (RiotSharpException ex)
{
    // I hope you have some real code here
    throw;
}

// now you can use it 
foreach(var player in game.Participants)
{

}

Note that your current try-catch block don't catch anything except RiotSharpException and even for that type of exceptions you simply rethrow it. 请注意,您当前的try-catch块除了RiotSharpException之外什么都不会捕获,即使对于这种类型的异常,您也可以将其重新抛出。 So nothing will change if you'll remove try-catch completely here 因此,如果您在此处完全删除try-catch则不会有任何改变

var api = RiotApi.GetInstance("KEY");
// if api can be null, then you can use null-propagation operation ?.
var game = api?.GetCurrentGame(RiotSharp.Platform.EUW1, 79200188);
if (game == null) // consider to add null-check
   return;

foreach(var player in game.Participants)
   // ...

Further reading: 3.7 Scopes from C# specification 进一步阅读: 3.7 C#规范的范围

The scope of a name is the region of program text within which it is possible to refer to the entity declared by the name without qualification of the name. 名称的范围是程序文本的区域,在其中可以引用名称声明的实体,而无需对该名称进行限定。 Scopes can be nested 范围可以嵌套

And especially 特别是

• The scope of a local variable declared in a local-variable-declaration (§8.5.1) is the block in which the declaration occurs. •在local-variable-declaration(第8.5.1节)中声明的局部变量的范围是声明所在的块。

So when you declare local variable within try-catch block, it can be referred only within try-catch block. 因此,当您在try-catch块中声明局部变量时,只能在try-catch块中引用它。 If you declare local variable within method body block, it can be referred within method body scope and within nested scopes. 如果在方法主体块中声明局部变量,则可以在方法主体范围内和嵌套范围内引用局部变量。

Something like this: 像这样:

private void button1_Click(object sender, EventArgs e)
{
    var api = RiotApi.GetInstance("KEY");

    // if we have api, try get the game
    var game = api != null 
      ? api.GetCurrentGame(RiotSharp.Platform.EUW1, 79200188)
      : null;

    // if we have game, process the players 
    if (game != null)
        foreach (var player in game.Participants) 
        {
            //TODO: put relevant logic here
        }
}

Please, notice, that try {} catch (RiotSharpException ex) {throw;} is a redundant construction and can be dropped . 请注意, try {} catch (RiotSharpException ex) {throw;}多余的结构,可以删除

Could you do something like this? 你能做这样的事情吗? I dont know what type your GetCurrentGame retuns from the api so I just used GameType as a placeholder. 我不知道您的GetCurrentGame从api返回什么类型,所以我只是使用GameType作为占位符。

private void button1_Click(object sender, EventArgs e)
{
    var api = RiotApi.GetInstance("KEY");

    GameType game = new GameType();        

    try
    {
        game = api.GetCurrentGame(RiotSharp.Platform.EUW1, 79200188);
    }

    catch (RiotSharpException ex)
    {
        throw;
    }

    if(game == null || !game.Participants.Any()) return;

    foreach (var player in game.Participants) // Can't find game variable
    {

    }
}

尝试这样的事情:

var game = (Object)null;

string y = null; 字符串y = null;

var x = y; var x = y;

this will work 这会工作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何声明带有我的名字的char变量? - How can I declare a char variable with my name on it? 如何全局声明一个变量,以便我可以在 mvc 中的所有其他 viwes 中访问它 - How can i declare a variable globally so that i can access it across all other viwes in mvc 当变量在 C# 中定义匿名 class 时,如何在块外声明变量 - How can I declare a variable outside of a block when the variable defines an anonymous class in C# 如何使用 Roslyn 声明 var 变量? - How do I declare a var variable with Roslyn? 如何声明一个带有连字符的变量 - How do I declare a variable with hyphen in it 如何声明一个空的 ClientContext - How to Declare an Empty ClientContext 如何在 ASP.NET 中使用 C# 声明变量? - How can I declare a variable using C# in ASP.NET? C#-如何在“通用类”类型的MainForm类中声明变量而不指定通用类型 - C# - How can I declare a variable in MainForm class of type “Generic Class” without specifing generic type 如何声明仅一个db变量,该变量将显示在Entities1.edmx或Entities2.edmx上 - How can I declare just one db variable that will look either to the Entities1.edmx or to Entities2.edmx 如何用我的名字声明一个字符串变量+将我的出生月份添加为数字? - How can I declare a string variable with my name + adding my birth month as a number?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM