引用类型参数传递测试

posted on 2004-08-13 13:28 hill 阅读(893) 评论(0)

引用类型参数传递测试(内容主要来自MSDN)

更详细的说明请参照MSDN"传递参数"

  1. 通过值传递引用类型

    例如传递数组change(string[] str),这种方式可以改变数组的值,相当于使用了ref,但是它和ref还是有区别的,这时如果使用new重新定义数组的维数,则改变是local的,不会影响到函数change以外的str

  2. 通过引用传递引用类型

    这种传递是明确使用ref,例如change(ref string[] str),这种方式和上面唯一的区别在于即使在函数内使用new重新定义数组也会影响到被引用的原始值,也就是change以外的str也随着change的改变而改变,是绝对的指针引用方式

以下是我的总结

-------------------------

  1. 对象是通过引用传递的,相当于前面使用ref
  2. 你可以在对象引用前加ref,这种调用和3的结果是一样的
  3. 对结构类型的参数是通过值传递的,函数内的改变不会影响函数外的原始值
  4. 接口相当于对象也是通过引用传递的,他的情况和3完全相同

具体测试代码如下:

using System;
using System.Data;
using System.Data.SqlClient;

namespace ConsoleApplication1
{
    class Class1
    {
        [STAThread]
        static void Main(string[] args)
        {
            //1.通过值传递引用类型
            Console.WriteLine("1.通过值传递引用类型");
            TestValueRef tvr = new TestValueRef();
            tvr.Test();

            //2.通过引用传递引用类型
            Console.WriteLine("2.通过引用传递引用类型");
            TestValueRefByRef tvrbr = new TestValueRefByRef();
            tvrbr.Test();

            //3.交换字符串

            //4.测试传递对象是否是引用类型
            Console.WriteLine("4.测试传递对象是否是引用类型");
            TestTest2 t = new TestTest2();
            t.Test();

            //5.测试传递结构是否是引用类型
            Console.WriteLine("5.测试传递结构是否是引用类型");
            TestStruct testStruct = new TestStruct();
            testStruct.Test();

            //6.测试传递接口是否是引用类型
            Console.WriteLine("6.测试传递接口是否是引用类型");
            TestInterface testInterface = new TestInterface();
            testInterface.Test();

            Console.Read();
        }
    }
    class TestValueRef
    {
        public void Test()
        {
            string[] str = new string[5] { "1", "2", "3", "4", "5" };
            print(str, "原始数据");
            change(str);
            print(str, "更改结束");
        }
        private void change(string[] str)
        {
            str[0] = "333";
            print(str, "更改数组的一个值");
            str = new string[6] { "0", "1", "2", "3", "4", "5" };
            print(str, "更改函数内改变数组的维数");
        }
        private void print(string[] str, string strMsg)
        {
            Console.WriteLine();
            Console.WriteLine(strMsg);
            foreach (string s in str)
            {
                Console.WriteLine(s);
            }
        }
    }

    class TestValueRefByRef
    {
        public void Test()
        {
            string[] str = new string[5] { "1", "2", "3", "4", "5" };
            print(str, "原始数据");
            change(ref str);
            print(str, "更改结束");
        }
        private void change(ref string[] str)
        {
            str[0] = "333";
            print(str, "更改数组的一个值");
            str = new string[6] { "0", "1", "2", "3", "4", "5" };
            print(str, "更改函数内改变数组的维数");
        }
        private void print(string[] str, string strMsg)
        {
            Console.WriteLine();
            Console.WriteLine(strMsg);
            foreach (string s in str)
            {
                Console.WriteLine(s);
            }
        }
    }
    class TestObjectRef
    {
        public string strTest = "this is a test";
    }
    class TestTest2
    {
        public void Test()
        {
            TestObjectRef o = new TestObjectRef();
            Console.WriteLine(o.strTest);
            Change(ref o);
            Console.WriteLine(o.strTest);
        }
        public void Change(ref TestObjectRef o)
        {
            o.strTest = "change object";
            Console.WriteLine(o.strTest);
        }
    }
    struct MyStruct
    {
        public string str1;
        public string str2;
        public string str3;
    }
    class TestStruct
    {
        public void Test()
        {
            MyStruct myStruct = new MyStruct();
            myStruct.str1 = "str1";
            myStruct.str2 = "str2";
            myStruct.str3 = "str3";
            print(myStruct, "更改前");
            Change(myStruct);
            print(myStruct, "更改后");
        }
        public void Change(MyStruct myStruct)
        {
            myStruct.str1 = "change1";
            myStruct.str2 = "change2";
            myStruct.str3 = "Change3";
            print(myStruct, "更改中");
        }
        public void print(MyStruct myStruct, string strMsg)
        {
            Console.WriteLine();
            Console.WriteLine(strMsg);
            Console.WriteLine(myStruct.str1);
            Console.WriteLine(myStruct.str2);
            Console.WriteLine(myStruct.str3);
        }
    }
    class TestInterface
    {
        public void Test()
        {
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = "test";
            Console.WriteLine(cmd.CommandText);
            IDbCommand Icmd = (IDbCommand)cmd;
            Change(Icmd);
            Console.WriteLine(cmd.CommandText);
        }
        public void Change(IDbCommand iDb)
        {
            iDb.CommandText = "change";
            Console.WriteLine(iDb.CommandText);
        }
    }
}
Contributors: FHL