• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            Where there is a dream ,there is hope

              C++博客 :: 首頁 :: 聯(lián)系 :: 聚合  :: 管理
              64 Posts :: 0 Stories :: 8 Comments :: 0 Trackbacks

            常用鏈接

            留言簿(1)

            我參與的團隊

            搜索

            •  

            最新評論

            閱讀排行榜

            評論排行榜

            原地址:http://www.cnblogs.com/DonLiang/archive/2008/02/16/1070717.html

            近段時間,有幾個剛剛開始學習C#語言的愛好者問我:C#中的函數(shù),其參數(shù)的傳遞,按值傳遞和按引用傳遞有什么區(qū)別。針對這一問題,我簡單寫了個示例程序,用以講解,希望我沒有把他們繞暈。因為,常聽別人說起:“你不說我還明白,你一說,我就糊涂了”。
                好,現(xiàn)在開始吧。
                我們知道,在C#中,類型有值類型(例如int)和引用類型(例如string)之分,傳遞參數(shù)有按值傳遞和按引用傳遞之分。這樣,簡單的組合一下,我們可以得到以下幾種傳遞方式:(1)按值傳遞值類型。(2)按值傳遞引用類型。(3)按引用傳遞值類型。(4)按引用傳遞引用類型。一般來說,除非使用特定的關(guān)鍵字(ref和out)否則參數(shù)是按值傳遞的。也就是說,會傳遞一個副本。傳遞副本的一個好處是,可以避免誤操作而影響了原始值。原因是在被調(diào)用的函數(shù)體內(nèi),操作的是副本的值,而不是原始值。當然,傳遞副本也是有副作用的,最為突出的應(yīng)該是由于復制而產(chǎn)生的性能損耗,這點在大型的值類型身上尤為突出。那么C#的編譯器的默認行為為什么不是使用按引用傳遞參數(shù)呢?呵呵,其實我沒仔細深入思考過這個問題。我猜測,是因為安全因素吧,就是怕函數(shù)誤操作了原始值。這點應(yīng)該和C#的編譯器要求顯示使用關(guān)鍵字(ref和out)差不多,都是為了清楚地表達使用的意圖,以避免誤操作。使用ref等關(guān)鍵字,暗示函數(shù)調(diào)用者知道,在函數(shù)體內(nèi),也許存在修改原始值的語句,會改變參數(shù)的值(或者叫狀態(tài))。
                用個簡單的示例演示一下。
                示例代碼如下所示:

            using System;
              
              
            namespace DonLiang
              
            {
                  
            class Sample
                  
            {
                      
                      
            public static void foo(int x)
                      
            {
                         x 
            = 10;
                     }

             
                     
            //*
                     public static void foo(ref int x)
                     
            {
                         x 
            = 10;
                     }

                     
            //*/
             
                     
            /**//*
                     public static void foo(out int x)
             22        {
             23            x = 10;
             24        }
             25        //
            */

                     
             
                     
                     
            public class Point
                    
            {
                         
            private int m_x;
                         
            private int m_y;
             
                         
            public Point()
                        
            {
                            m_x 
            = 0;
                            m_y 
            = 0;
                        }


                        
            public Point(int x, int y)
                       
            {
                            m_x 
            = x;
                            m_y 
            = y;
                         }


                        
            public void Change(int x, int y)
                        
            {
                           m_x 
            = x;
                           m_y 
            = y;
                        }


                        
            public override string ToString()
                        
            {
                           
            return string.Format("The Point is ({0},{1})", m_x.ToString(), m_y.ToString());
                        }

                    }

                  

                   
                    
            public static void foo(Point p)
                    
            {
                        p.Change(
            1010);
                    }


                    
            public static void foo(ref Point p)
                    
            {
                        p.Change(
            100100);
                   }


                    
            public static void other(Point p)
                    
            {
                        Point tmp 
            = new Point(1316);
                       p 
            = tmp;
                    }


                    
            public static void other(ref Point p)
                    
            {
                        Point tmp 
            = new Point(138168);
                        p 
            = tmp;
                    }

                 

                   
                    
            static void Main(string[] args)
                    
            {
                        
            int n = 5;

                        
            //call the foo(int x) method and check what happened.
                       Console.WriteLine("before call foo(int x) the n = " + n.ToString());
                        foo(n);
                        Console.WriteLine(
            "after call foo(int x) the n = " + n.ToString());

                        Console.WriteLine(
            "--------------------------------------------------------------");

                       
            //call the foo(ref int x) method and check what happened.
                        Console.WriteLine("before call foo(ref int x) the n = " + n.ToString());
                       foo(
            ref n);
                        
            //foo(out n);
                        Console.WriteLine("after call foo(ref int x) the n = " + n.ToString());
                        Console.WriteLine(
            "--------------------------------------------------------------");
                        Point p 
            = new Point(55);
                       Point q 
            = p;

                       
            //call the foo(Point p) method and check what happened.
                        Console.WriteLine("before call foo(Point p) the p = " + p.ToString());
                        foo(p);
                       Console.WriteLine(
            "after call foo(Point p) the p = " + p.ToString());
                        Console.WriteLine(
            "q = " + q.ToString());

                        Console.WriteLine(
            "--------------------------------------------------------------");

                        
            //call the foo(ref Point p) method and check what happened.
                        Console.WriteLine("before call foo(ref Point p) the n = " + p.ToString());
                        foo(
            ref p);
                        Console.WriteLine(
            "after call foo(ref Point p) the n = " + p.ToString());
                        Console.WriteLine(
            "q = " + q.ToString());

                     Console.WriteLine(
            "--------------------------------------------------------------");

                        
            //call the other(Point p) method and check what happened.
                       Console.WriteLine("before call other(Point p) the n = " + p.ToString());
                       other(p);
                       Console.WriteLine(
            "after call other(Point p) the n = " + p.ToString());
                       Console.WriteLine(
            "q = " + q.ToString());

                        Console.WriteLine(
            "--------------------------------------------------------------");

                       
            //call the other(ref Point p) method and check what happened.
                       Console.WriteLine("before call other(ref Point p) the n = " + p.ToString());
                        other(
            ref p);
                       Console.WriteLine(
            "after call other(ref Point p) the n = " + p.ToString());
                    Console.WriteLine(
            "q = " + q.ToString());

                        Console.ReadLine();
                    }

                    
                }

            }



            按值傳遞引用類型 和 按引用傳遞引用類型
                之所以把這兩個放在一起講,是因為,如結(jié)果圖所示,兩種傳遞方式,都成功修改了值——這兩個函數(shù)都分別調(diào)用了一個輔助修改的函數(shù)Change,去修改內(nèi)部狀態(tài),即m_x,m_y的值,從5到10。呃,竟然都可以成功修改原始值,那么,為什么會存在兩種方式呢?它們有什么區(qū)別嗎?分別用在什么地方?為了說明他們的區(qū)別,我特意寫了兩個名為other的函數(shù),在函數(shù)內(nèi)new一個Point對象,并使從參數(shù)傳遞過來的引用這個新生成的Point對象。值得提醒的是,這個引用其定義在函數(shù)體外。其運行如上圖我用方框框起來那個。
                可以很清楚地看到,通過值傳遞方式,可以改變其值,卻不能改變其本身所引用的對象;而按引用傳遞方式可以。

                順便提一下,代碼中,有一段注釋掉的代碼,使用out關(guān)鍵字的。當你嘗試將其兩者一起寫著,然后,編譯,C#編譯器是會提示錯誤的(error CS0663: 'foo' cannot define overloaded methods that differ only on ref and out)。其原因是,C#編譯器,對ref和out生成的IL代碼,是相同的;而在CLR層面,是沒有ref和out的區(qū)別的。C#中,ref和out的區(qū)別,主要是,誰負責初始化這個參數(shù)使之能用——ref形式是函數(shù)外初始化,而out是函數(shù)內(nèi)初始化。

            posted on 2010-12-03 09:38 IT菜鳥 閱讀(520) 評論(0)  編輯 收藏 引用 所屬分類: C#
            久久国产精品一区二区| 亚洲国产精品无码久久一线| 国产精品一区二区久久不卡| 无码八A片人妻少妇久久| 久久精品亚洲男人的天堂| 国产69精品久久久久9999| 国产亚洲美女精品久久久| 国产成人无码精品久久久久免费 | 亚洲国产精品无码久久青草| 久久99国产精品久久久| 久久久久国产精品| 精品乱码久久久久久夜夜嗨| 久久性精品| 97久久国产综合精品女不卡| 亚洲国产欧洲综合997久久| 91视频国产91久久久| 99久久精品国产一区二区三区 | 国产成人精品久久二区二区| 国产精品一久久香蕉产线看 | 精品无码久久久久久久动漫 | 亚洲精品午夜国产VA久久成人| 久久天堂AV综合合色蜜桃网| 99久久99久久精品免费看蜜桃| 久久se精品一区二区| 欧美午夜精品久久久久久浪潮| 久久综合鬼色88久久精品综合自在自线噜噜 | 久久久久99精品成人片试看| 99久久精品国产综合一区| 久久久午夜精品| 99久久亚洲综合精品网站| 久久综合鬼色88久久精品综合自在自线噜噜 | 亚洲精品无码久久千人斩| 日本精品久久久久中文字幕8 | 久久久久亚洲AV成人网人人网站| 色综合久久中文字幕无码| 久久久久亚洲爆乳少妇无 | 国产成人精品久久免费动漫| 久久精品国产精品亚洲下载| 久久人妻少妇嫩草AV无码专区| 久久精品国产福利国产秒| 国产成年无码久久久免费|