我对 C# 很陌生,并且对变量范围感到困惑。这是我编写的代码块,但是当我尝试在 try 块之外访问 a 和 b 的值时,它给了我编译时错误
class TestConditionalStatements
{
static void Main(string[] args)
{
int a, b;
try
{
a = 10;
b = 20;
}
catch (Exception e)
{
Console.Write(e.Message);
}
//This line gives compile time error
ConditionalStatements c = new ConditionalStatements(a, b);
string result;
c.checkValidity(c, out result);
Console.WriteLine(result);
}
}
Run Code Online (Sandbox Code Playgroud)