• λ我爱Aspx >> Asp.Net >> c#中使用ref和out一点认识
  • c#中使用ref和out一点认识

  • :未知  Դ:非寒日志  :2007-4-21 0:45:49  ؼ:c#
  • 首先,如果不使用这两个关键字,那是什么样 呢?

    看下面的例子:

    using system;

    class test

    <

    static void swap(ref int x, ref int y)

    <

    int temp = x;

    x = y;

    y = temp;

    >

    static void swap(int x,int y)

    <

    int temp = x;

    x = y;

    y = temp;

    >

    static void main()

    <

    int i = 1, j = 2;

    swap(ref i, ref j);

    console.writeline("i = <0>, j = <1>", i, j);

    swap(i,j);

    console.writeline("i = <0>, j = <1>", i, j);

    >

    >

    程序经编译后执行输出:

    i = 2, j = 1

    i = 2, j = 1

    这是csdn的一篇帖子上的例子.其实如果放在一起,并不能很容易的看出使用ref和不使用ref的区别.

    分开看,就很明显了.

    使用ref:

    using system;

    class test

    <

    static void swap(ref int x, ref int y)

    <

    int temp = x;

    x = y;

    y = temp;

    >

    static void main()

    <

    int i = 1, j = 2;

    swap(ref i, ref j);

    console.writeline("i = <0>, j = <1>", i, j);

    >

    >

    程序经编译后执行输出:

    i = 2, j = 1

    不使用:

    using system;

    class test

    <

    static void swap(int x,int y)

    <

    int temp = x;

    x = y;

    y = temp;

    >

    static void main()

    <

    int i = 1, j = 2;

    Ҷƪл˵?
  • һƪc#学习体会:使用 ref 和 out 传递数组
    һƪAjax在.NET中与Server控件的交互