C# ref對值類型和引用類型的影響

參考文章:http://www.cnblogs.com/slmk/archive/2012/03/19/2406429.html
說明:https://msdn.microsoft.com/zh-cn/library/14akc2c7.aspxhtml

探究ref對值類型和引用類型的影響c#

測試代碼:函數

public class TestRefClass
    {
        public void TestPrint()
        {
            Console.WriteLine("\n-------------- 對比1 ---------------");
            // ref 對 值類型的做用
            int testInt = 0;
            Console.WriteLine("值類型 = " + testInt);
            
            TestFunc1(testInt);
            Console.WriteLine("測試1  = "+ testInt);
            
            testInt = 0;
            TestFunc2(ref testInt);
            Console.WriteLine("測試2 = " + testInt);


            Console.WriteLine("\n-------------- 對比2 ---------------");
            //ref 對 引用類型的做用
            DataClass m_ref = new DataClass();
            Console.WriteLine("原值 = " + m_ref.GetHashCode() + "       content = " + m_ref.content);
            TestFunc3(m_ref);
            Console.WriteLine("測試3  = " + m_ref.GetHashCode() + "       content = " + m_ref.content);

            m_ref = new DataClass();
            Console.WriteLine("原值 = " + m_ref.GetHashCode() + "       content = " + m_ref.content);
            TestFunc4(ref m_ref);
            Console.WriteLine("測試4  = " + m_ref.GetHashCode() + "       content = " + m_ref.content);

            Console.WriteLine("\n-------------- 對比3 ---------------");
            m_ref = new DataClass();
            Console.WriteLine("原值 = " + m_ref.GetHashCode() + "       content = " + m_ref.content);
            TestFunc5(m_ref);
            Console.WriteLine("測試5  = " + m_ref.GetHashCode() + "       content = " + m_ref.content);

            m_ref = new DataClass();
            Console.WriteLine("原值 = " + m_ref.GetHashCode() + "       content = " + m_ref.content);
            TestFunc6(ref m_ref);
            Console.WriteLine("測試6  = " + m_ref.GetHashCode() + "       content = " + m_ref.content);
        }

       void TestFunc1(int value)
        {
            value = 1;
        }

        void TestFunc2(ref int value)
       {
           value = 2;
       }

        void TestFunc3(DataClass dataClass)
        {
            dataClass.content = 3;
        }

        void TestFunc4(ref DataClass dataClass)
        {
            dataClass.content = 4;
        }

        void TestFunc5(DataClass dataClass)
        {
            dataClass = new DataClass();
            dataClass.content = 5;
            Console.WriteLine("\t方法TestFunc5  = " + dataClass.GetHashCode() + "       content = " + dataClass.content);

        }

        void TestFunc6(ref DataClass dataClass)
        {
            dataClass = new DataClass();
            dataClass.content = 6;
            Console.WriteLine("\t方法TestFunc6  = " + dataClass.GetHashCode() + "       content = " + dataClass.content);

        }
    }

    public class DataClass
    {
        public int content = 0;
    }

測試打印:

總結:
函數參數是引用類型時,沒用使用ref傳參,對參數成員的修改會反饋到參數對象(對比2);若從新初始化參數再修改參數成員則不會影響到原來的參數對象(對比3),會在方法內建立一個新的副本對象。
使用ref傳參時,對參數成員的修改會反饋到參數對象(對比2);若從新初始化參數再修改參數成員(對比3),會建立一個新的存儲副本,並將原參數對象指向新的存儲地址。測試

相關文章
相關標籤/搜索