【转】C# 参数传递 引用类型与值类型的值传递和引用传递

///变长参数的例子
     class Program
    {
        static void Main(string[] args)
        {
            ShowAgeSum("w", 1, 2, 3, 4);
            ShowAgeSum("h", 1, 1, 1);
            ShowAgeSum("c", 99, 1, 2, 3, 4, 1, 0, 0, 0, 0, 0, 0, 100000);
            Console.Read();
        }

        static void ShowAgeSum(string team, params int[] ages)
        {
            int ageSum = 0;
            for (int i = 0; i < ages.Length; i++)
                ageSum += ages[i];
            Console.Write("{0}'s age is {1}\r\n",team,ageSum);
        }
    }
    ///值类型参数 按值传递 和按引用传递的区别 
    class Program
    {
        static void Main(string[] args)
        {
            int a = 0;
            Add(a);
            Console.WriteLine(a);
            Add(ref a);
            Console.WriteLine(a);
            Console.Read();
        }

        static void Add(int i)
        {
            i = i + 10;
            Console.WriteLine(i);
        }

        static void Add(ref int i)
        {
            i = i + 10;
            Console.WriteLine(i);
        }
    }
    结果:10,0,10,10///引用类型参数 按值传递和按引用传递的区别
    class Program
    {
        static void Main(string[] args)
        {
            ArgsByRef a = new ArgsByRef();
            Add(a);
            Console.WriteLine(a.i);
            Add(ref a);
            Console.WriteLine(a.i);
            Console.Read();
        }

        static void Add(ArgsByRef a)
        {
            a.i = 20;
            Console.WriteLine(a.i);
        }

        static void Add(ref ArgsByRef a)
        {
            a.i = 30;
            Console.WriteLine(a.i);
        }

       
    }
    class ArgsByRef
    {
        public int i = 10;
    }
结果 :20,20,30,30///字符串参数的按值与按引用传递 与 值类型一致 (string 是引用类型)
 class Program
    {
        static void Main(string[] args)
        {
            string a = "Old String";
            Add(a);
            Console.WriteLine(a);
            Add(ref a);
            Console.WriteLine(a);
            Console.Read();
        }

        static void Add(string a)
        {
            a = "new String";
            Console.WriteLine(a);
        }

        static void Add(ref string a)
        {
            a = "new String";
            Console.WriteLine(a);
        }
    }
    结果:new String, Old String,new String,new String;

以上内容转自:http://www.cnblogs.com/whc-blog/archive/2011/07/20/2111803.html
感觉描述得非常清楚,不过引用类型的按值传递与按引用传递没有说清楚,下面做个补充:

class Program
{
static void Main(string[] args)
{
MyTest mt = new MyTest();
//引用类型按值传递,传递的是该引用所指向的对象。
Func(mt);
//可以改变其值(因为是同一对象),但不能让引用指向新的对象(因为没有传递mt的引用过去)
Console.WriteLine(mt.Name);//结果输出:张三
}
static void Func(MyTest test)
{
test.Name = "张三";
test = new MyTest();
test.Name = "李四";
}
}
class MyTest
{
public string Name;
}

再与下面的代码对比:

    class Program
    {
        static void Main(string[] args)
        {
            MyTest mt = new MyTest();
            //引用类型按引用传递,传递对象引用
            Func(ref mt);
            //因为引用都过去了,可以重新指向新对象,也可以改变引用所指向对象的值
            Console.WriteLine(mt.Name);//结果输出:李四

        }

        static void Func(ref MyTest test)
        {
            test.Name = "张三";
            test = new MyTest();
            test.Name = "李四";
        }
    }

    class MyTest
    {
        public string Name;
    }

以上一段来自:http://bbs.csdn.net/topics/390246586 5、6楼

posted @ 2012-12-07 17:29  伯箫  阅读(1382)  评论(0)    收藏  举报