关于C#的问题,想问一下朋友们,下面的代码为啥会出现转义范围问题。错误代码CS8374
class Simple
{
private int score=5;
public ref int ReturnValue ()
{
return ref score;
}
public void PrintValue()
{
Console.WriteLine($"The score is {score}");
}
}
class Program
{
static void Main(string[] args)
{
int x = 2;
int y = 3;
Simple simple = new Simple();
simple.PrintValue();
ref int newScore = ref simple.ReturnValue();
newScore = ref x; //该代码会出现转义范围问题
//如下步骤将不会产生转义范围问题
//ref int newScore = ref x;
//newScore = 7;
//Console.WriteLine(x);
//newScore = ref simple.ReturnValue();
//newScore = 15;
//simple.PrintValue();
//newScore = ref y;
//Console.WriteLine(newScore);
}
}