引用类型传递参数的特殊情况(string)

 

 

    struct Patient
    {
        public string status;
    }
    class Person
    {
        public string life;
    }
    class Program
    {
        static void Main(string[] args)
        {
            Patient ps = new Patient();
            ps.status = "我生病了";
            SeeDoctor(ps.status);
            Console.WriteLine("结构类型patient的status:{0}", ps.status);
            //ps.status的结果为"我生病了"

            Person pc = new Person();
            pc.life = "生病了";
            SeeDoctor(pc.life);
            Console.WriteLine("类类型Person的life:{0}", pc.life);
            //pc.life的结果为"我生病了".
            Console.ReadKey();
        }
        static void SeeDoctor(string temp)
        {
            temp = "病好了";
        }
    }

 (图中: 线程栈误写成了"线型栈",另外temp变量是在SeeDoctor方法的线程栈上,而ps 和pc 两个变量是在Main方法的线程栈上.)

图解:

  

图解:

struct Patient
    {
        public string status;
    }
    class Person
    {
        public string life;
    }
    class Program
    {
        static void Main(string[] args)
        {
            Patient ps = new Patient();
            ps.status = "我生病了";
            SeeDoctor( out ps.status);
            Console.WriteLine("结构类型patient的status:{0}", ps.status);
            //ps.status的结果为"病好了"

            Person pc = new Person();
            pc.life = "生病了";
            SeeDoctor(out pc.life) ;
            Console.WriteLine("类类型Person的life:{0}", pc.life);
            //pc.life的结果为"病好了".
            Console.ReadKey();
        }
        static void SeeDoctor( out string temp)
        {
            temp = "病好了";
        }
    }

 图解(此时,结构与结构基本一致):

posted @ 2013-12-02 21:31  我叫小菜  阅读(243)  评论(0编辑  收藏  举报