C#基礎系列第七篇

來源:互聯網
上載者:User

前言: 完全是我在學習過程中記錄的筆記,只不過分享一下讓很多剛開始學習.net編程的人能夠很快的學會C#語言

  1. out和ref

(1) 方法中的變數都是局部變數,使用如果需要保證方法中的資料在外界還能使用,那麼可以考慮傳回值return,但是return只能返回一個資料,如果考慮多個返回資料,就不行了

class Person{public string State;}class Program{static void Main(string[] args){Person p;Create1(out p); //將p的地址傳遞給Person}public static void Create1(out Person p) //表示傳遞Person類{p = new Person();}}

(2)可以使用參數返回,out就是參數返回的一種

            1)方法外定義一個變數

            2)方法參數寫為"...out 參數名..."

            3)調用,在方法中為這個變數賦值

            4)在方法調用後,方法外的這個變數就包含了方法中賦的值

                static void Main(string[] args)

                   {

                          int num;

                          GetNum(out num);

                          Console.WriteLine(num);

                   }

                   static  void GetNum(out int num)

                   {

                          num = 10;

                   }

     (3)ref同樣可以使用這個功能

            1)ref在使用前要賦值

            2)out必須在方法中賦值

     (4)一般使用ref是使用方法修改某些資料以後,要求方法結束後還能使用

     (5)一般使用out是為了使用某個方法為某些變數進行初始化

     (6)ref使用前賦值,out方法中賦值

  1. 一個案例:類比登陸

(1))返回登入是否成功,如果登入失敗,提示使用者使用者名稱錯誤還是密碼錯誤

                 class Program{static void Main(string[] args){while (true){//輸入使用者名稱密碼Console.Write("請輸入使用者名稱:");string uid = Console.ReadLine();Console.Write("請輸入密碼:");string pwd = Console.ReadLine();string msg;//判斷if (Logining(uid, pwd, out msg)){Console.WriteLine(msg);}else{Console.WriteLine(msg);}}}static bool Logining(string uid, string pwd,out string msg){if (uid == "admin" && pwd == "123456"){msg = "登入成功";return true;}else if (uid == "admin"){msg = "密碼錯誤";return false;}else{msg="使用者名稱錯誤";return false;}}}
  1. 可變參數

(1) 如果參數是一個數組,那麼在傳參的時候必須定義一個數組出來

         class Program

            {

                   static void Main(string[] args)

                   {

                          int[] nums = { 1, 2, 3, 5, 4, 65, 76, 786, 8, 9, 89 };

                          GetArrayList(nums);

                          GetArrayList(); //傳遞一個長度為0的數組

                          GetArrayList(1, 2, 3, 5, 4, 65, 76, 7, 34, 65, 76, 86, 8, 9, 89);

                          Console.ReadKey();

                   }

                   public static void GetArrayList(params int[] nums)

                   {

                          Console.WriteLine(nums.Length);

                   }

            }

(2) 如果方法有多個參數params只能放在最後,並且方法中只允許有一個params數組

            static void Main(string[] args)

        {

            int[] nums = { 1, 2, 3, 5, 4, 65, 76, 786, 8, 9, 89 };

            OutPut(5, 23, 43, 54, 54, 65);

            Console.ReadKey();

        }

        //用一個方法完成數組的輸出,指定輸出多少項

        public static void OutPut(int count, params int[] nums)

        {

            for (int i = 0; i < count; i++)

            {

                Console.Write("{0} ", nums[i]);

            }

        }

        //public static void OutPut(int n1, int n2, int n3, int n4, int n5,int n6)

        //{

        //}

  1. 重載的方法提供轉換的實現

(1) 定義隱式轉換使用關鍵字  implicit

 (2)定義顯示轉換使用關鍵字  explicit

 (3)轉換順序和定義順序相同

            變數1=變數2;

            對應參數 類型1(類型2 value);

(4)文法

            [存取修飾詞] static [implicitlexplcit] operator 目標類型(待轉類型 value)

            {

                   目標類型 o=new 目標類型();

            //賦值操作,顯示轉換中可以將賦值操作作用checked{..}塊包起來,如放不下就異常

                   return 0;

            }

class Program{static void Main(string[] args){Perosn p = new Perosn();p.Age = 23;int num = (int)p;//Console.WriteLine(p);Console.WriteLine(num);}}class Perosn{int age;public int Age{get { return age; }set { age = value; }}//public static implicit operator int(Perosn value)//{        //    int n = value.age;        //    return n;//}public static explicit operator int(Perosn value){int n = value.age;return n;}}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.