• C# 中i++在ref参数调用下的有趣现象


    废话少说,今天自己下了 ref 与 out 例子,发现了如下问题,先看代码

    class Program
    {
    static void Main(string[] args)
    {
    int i = 0;
    int x = 0;
    RefTest(ref i);
    RefTest1(ref x);
    int j,y;
    OutTest(out j);
    OutTest1(out y);
    Console.WriteLine("J--->value:" + j);
    ParamTest();
    ParamTest(1, 2, 3);
    ParamTest(4, 5, 6, 7);
    Console.Read();

    }

    static void RefTest(ref int i)
    {
    i = i + 1;
    Console.WriteLine(i);
    }

    static void RefTest1(ref int x)
    {
    x = x++;
    Console.WriteLine(x);
    }

    static void OutTest(out int j)
    {
    j = 0;
    j = j +1;
    Console.WriteLine(j);
    }

    static void OutTest1(out int y)
    {
    y = 0;
    y = y ++;
    Console.WriteLine(y);
    }

    static void ParamTest(params int[] iList)
    {
    if (iList.Length > 0)
    {
    Console.WriteLine("Params Number is:" + iList.Length);
    for (int i = 0; i < iList.Length; i++)
    {
    Console.Write("Params[{0}]--->value:", i);
    Console.WriteLine(iList[i]);

    }
    }
    else
    {
    Console.WriteLine("Params Not is Used");
    }
    }
    }

    运行以上结果 :

    大家 可以看一下 在调用 同等的 refTest(),outtest()与refTest1(),outTest1()方法的时候,唯一区别的地方是 i=i+1;与i=i++这两处,结果完全迥异,不明白为什么,希望高手解答。

    在用 ref 与 out的时候 i++ 失效了。

  • 相关阅读:
    Mysql热备份
    win10 上安装虚拟机
    SpringMVC AJAX向后台传递数组参数/实体集合
    解决eclipse中tomcat不加载web项目的问题
    Python 基础第九天
    Python 基础第8天(文件管理)
    Python 基础第七天
    Python 基础第六天
    Python 基础第五天
    Python 基础第四天
  • 原文地址:https://www.cnblogs.com/BinaryBoy/p/2280524.html
Copyright © 2020-2023  润新知