天天看点

值类型+引用类型+ref

1、准备

    struct Str

    {

        public int i;

    }

    class Cla

2、赋值

    Str str = new Str();

    str.i = 10;

    Cla cla = new Cla();

    cla.i = 10;

分类:

运算A

   static void F(Str str)

   {

     str.i = 100;

   }

   static void F(Cla cla)

     cla.i = 100;

结果A

            F(str);

            Console.WriteLine(str.i);//结果10

            F(cla);

            Console.WriteLine( cla.i);//结果100

运算B

   static void F(ref Str str)

   static void F(ref Cla cla)

结果B

            F(ref str);

            Console.WriteLine(str.i);//结果100

            F(ref cla);

运算C

     str=new Str();

     cla=new Cla();

结果C

            Console.WriteLine( cla.i);//结果10

运算D

结果D

本文转自桂素伟51CTO博客,原文链接: http://blog.51cto.com/axzxs/810492,如需转载请自行联系原作者

继续阅读