コード例 #1
0
        static void Main(string[] args)
        {
            //=======================================
            //字串參數傳遞問題重現 測試:
            //=======================================
            string strX = "ABC";

            //呼叫 Change
            Console.ForegroundColor = ConsoleColor.Gray;
            Console.WriteLine("[Main](before call Change()) strX={0}", strX);
            Change(strX);
            Console.WriteLine("[Main](after call Change()) strX={0}", strX);

            //呼叫 ChangeByRef
            Console.ForegroundColor = ConsoleColor.DarkGray;
            Console.WriteLine("[Main](before call ChangeByRef()) strX={0}", strX);
            ChangeByRef(ref strX);
            Console.WriteLine("[Main](after call ChangeByRef()) strX={0}", strX);

            //=======================================
            //Value Type 測試:
            //=======================================
            int id = 1;		//##1.1##
            //呼叫 ValueTypeCall
            Console.ForegroundColor = ConsoleColor.Green;
            Console.WriteLine("[Main](before call ValueTypeCall()) id={0}", id);
            ValueTypeCall(id);
            Console.WriteLine("[Main](after call ValueTypeCall()) id={0}", id);		//##1.4##

            //呼叫 ValueTypeCallByRef
            Console.ForegroundColor = ConsoleColor.DarkGreen;
            Console.WriteLine("[Main](before call ValueTypeCallByRef()) id={0}", id);
            ValueTypeCallByRef(ref id);
            Console.WriteLine("[Main](after call ValueTypeCallByRef()) id={0}", id);	//##2.4##

            //=======================================
            //Reference Type 測試:  (String)
            //=======================================
            string str = "ABC";			//##3.1##
            //呼叫 StringTypeCall
            Console.ForegroundColor = ConsoleColor.Blue;
            Console.WriteLine("[Main](before call StringTypeCall()) str={0}", str);
            StringTypeCall(str);
            Console.WriteLine("[Main](after call StringTypeCall()) str={0}", str);		//##3.4##

            //呼叫 StringTypeCallByRef
            Console.ForegroundColor = ConsoleColor.DarkBlue;
            Console.WriteLine("[Main](before call StringTypeCallByRef()) str={0}", str);
            StringTypeCallByRef(ref str);
            Console.WriteLine("[Main](after call StringTypeCallByRef()) str={0}", str);	//##4.4##

            //=======================================
            //Reference Type 測試:  (Object)
            //=======================================
            //呼叫 ObjectTypeCall
            Console.ForegroundColor = ConsoleColor.Yellow;
            Customer cust = new Customer { Id = 1, Name = "Tester" };		//##5.1##
            Console.WriteLine("[Main](before call ObjectTypeCall()) Id={0}, Name={1}", cust.Id, cust.Name);
            ObjectTypeCall(cust);
            Console.WriteLine("[Main](after call ObjectTypeCall()) Id={0}, Name={1}", cust.Id, cust.Name);	//##5.4##

            //呼叫 ObjectTypeCallByRef
            Console.ForegroundColor = ConsoleColor.DarkYellow;
            cust = new Customer { Id = 1, Name = "Tester" };		//##6.1##
            Console.WriteLine("[Main](before call ObjectTypeCallByRef()) Id={0}, Name={1}", cust.Id, cust.Name);
            ObjectTypeCall(ref cust);
            Console.WriteLine("[Main](after call ObjectTypeCallByRef()) Id={0}, Name={1}", cust.Id, cust.Name);	//##6.4##

            Console.ReadLine();
        }
コード例 #2
0
 //##6.2##
 private static void ObjectTypeCall(ref Customer inCust)
 {
     inCust.Name = "Jasper";		//##6.3##
     Console.WriteLine("[ObjectTypeCall] Id={0}, Name={1}", inCust.Id, inCust.Name);
 }