惟独月色依旧 2022-09-08 17:19
浏览 25
已结题

关于C#的代码,出现了转义范围问题

关于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);


    }
}
  • 写回答

0条回答 默认 最新

    报告相同问题?

    问题事件

    • 系统已结题 9月16日
    • 创建了问题 9月8日

    悬赏问题

    • ¥20 找辅导 初学者 想实现一个项目 没有方向
    • ¥15 关于渗漏场的电场分布模拟
    • ¥24 matlab怎么修改仿真初始时间
    • ¥15 两分段线性回归模型分析阈值效应
    • ¥15 前端和后端代码都没报错,但是点登录没反应的?
    • ¥100 需要远程解决QSQLITE问题!
    • ¥15 利用光场表达式画出初始光场强度分布图像等几个问题在这两个图片里
    • ¥15 gozero求手把手教学,400一天
    • ¥15 泥浆冲清水的泥浆分布
    • ¥15 LASSO回归分析筛选关键基因,适合多大样本量?