可变范围混淆C#

时间:2015-09-09 08:38:54

标签: c#

我对范围有疑问。

为什么我不能返回d而不能访问c?

public string Test(string t)
{
    var x = someoutcome();

    //test to be sure of outcome
    if(x != null)
    {
        var b = "A string to be returned" + t;
        var c = anothermethod();
        if(typeof(c) == string)
        {
            var d = "Hurray c and b are strings" + b;
        }
    }
    return d;
}

6 个答案:

答案 0 :(得分:3)

C#规范,第3.7节

  

在local-variable-declaration中声明的局部变量的范围   是声明发生的块。

这是什么意思?

d变量的范围从这里开始:

if(typeof(c) == string)
{

}结束。这称为block。 您的变量d - s范围是提到的if的块。

答案 1 :(得分:0)

您正在访问正确范围内的c,我在此处看不到任何错误。您无法返回d的原因非常明显:它是在不包含return d的范围内定义的。

public string Test(string t)
{
    var x = someoutcome();

    //test to be sure of outcome
    if(x != null)
    {
        var b = "A string to be returned" + t;
        var c = anothermethod();
        if(typeof(c) == string)
        {
            var d = "Hurray c and b are strings" + b;
        } // ** FROM HERE ON, d WONT BE ACCESSIBLE ANYMORE **
    }  // ** FROM HERE ON, b & c WONT BE ACCESSIBLE ANYMORE **
    return d;
}

但您可以直接返回d,而不是将其写入变量:

public string Test(string t)
{
    var x = someoutcome();

    //test to be sure of outcome
    if(x != null)
    {
        var b = "A string to be returned" + t;
        var c = anothermethod();
        if(typeof(c) == string)
        {
            return "Hurray c and b are strings" + b;
        }
    }
    return string.Empty;
}

请查看this site以了解有关范围内变量的更多信息。

答案 2 :(得分:0)

  

为什么我不能回复d

参考' d'仅存在于if块中,并在完成后丢失。

  

为什么我无法访问c?

这是一个完全不同的故事。 typeof运算符用于获取System.Type对象,并期望类型作为参数而不是变量或引用类型的实例。你的''只是一个包含String类型引用的变量。将typeof(c)更改为typeof(String)c.GetType(),您就会看到它现在有效

答案 3 :(得分:0)

其他答案解释范围。以下是您的代码的固定版本:

$('#newWindow').open(); //does not work

或简单地(没有定义public string Test(string t) { var x = someoutcome(); // define d here to have it available for return at the end of method block // note: you have to provide default value to return in case one of following "if" conditions are not true string d == null; if(x != null) { var b = "A string to be returned" + t; var c = anothermethod(); if(c.GetType() == typeof(string)) d = "Hurray c and b are strings" + b; } return d; } ):

d

答案 4 :(得分:0)

阻止声明变量的范围以{开头,以}

结尾
  

如果在块构造(如If语句)中声明变量,则该变量的作用域仅在块结束之前。生命周期是程序结束

查看变量范围https://msdn.microsoft.com/en-us/library/ms973875.aspx

答案 5 :(得分:0)

是的,问题实际上是var d的范围。在}之后var d变量不再存在之后,您尝试将其返回。这将是一个例外。

  1. 第一个选项
  2. 您可能需要在与var d相同的级别声明var x,它可以在if(x != null){...}循环中访问。你需要给它一个默认值,因为如果你不想返回d并且如果x == Null ???你必须预料到它,d将为null,你不能返回null;

    1. 第二个选项
    2. 您在循环if(x != null){...}中返回d,在}之后返回默认值,如下所示:

      public string Test(string t)
      {
          var x = someoutcome();
      
          //test to be sure of outcome
          if(x != null)
          {
              var b = "A string to be returned" + t;
              var c = anothermethod();
              if(typeof(c) == string)
              {
                  var d = "Hurray c and b are strings" + b;
                  return d;//d exist only in this loop, and is return, the method is stoped here
              }
          }
          return empty.string;//if the code can't return d (x==Null or typeof(c) != string)
      }
      
相关问题