.Net基礎加強04

來源:互聯網
上載者:User

標籤:style   blog   color   使用   for   re   

1.物件導向多態:

virtual

abstract

介面

2.實值型別,參考型別

3.值傳遞,引用傳遞(ref)

4.介面 

int(C#推薦用)      int32

5.枚舉  -----標誌枚舉

6.裡氏替換原則   

7.異常 try -  catch - finally{}

函數傳回值(函數參數前的修飾符)

Params 可變參數,無論有幾個參數,必須出現在參數列表的最後。可以為可變參數直接傳遞一個對應類型的數組

  class Program    {        static void Main(string[] args)        {            //int sum= Add(10,20);            //Console.WriteLine(sum); //30            //Console.ReadKey();            //Console.WriteLine("你好,我叫{0},今年{1}歲了,駕齡:{2}年了","葉長種",25,0);            //Console.ReadKey();            //int sum = Add(10, 20, 30);            int[] arrInt = { 1, 3, 5, 7, 9 };            //由於可變參數本身就是一個數組,所以可以直接傳遞一個數組進來            int sum = Add(arrInt);            Console.WriteLine(sum);            Console.ReadKey();        }        //可變參數即便使用者不傳遞任何參數,也可以(不傳參數則數組是一個長度為0的數組,不是null)        //可變參數只能出現在方法參數列表的最後一個        static int Add(params int[] nums)        {            int sum = 0;            for (int i = 0; i < nums.Length; i++)            {                sum += nums[i];            }            return sum;        }        //static int Add(int n, int m)        //{        //    return n + m;        //}        //static int Add(int n, int m, int l)        //{        //    return n + m + l;        //}    }

ref僅僅是一個地址,引用地址,可以把值傳遞強制改為引用傳遞。

out讓函數可以輸出多個值;

 class Program    {        static void Main(string[] args)        {            int basicSalary = 1000;            //ref的參數,要求傳遞進來的變數必須聲明,並且賦值。            JiangjinNum1(ref basicSalary);            JiangjinNum2(ref basicSalary);            BaoXian(ref basicSalary);            Console.WriteLine(basicSalary);            Console.ReadKey();        }        //獎金,優秀員工獎        static void JiangjinNum1(ref int salary)        {            salary += 500;        }        static void JiangjinNum2(ref int salary)        {            salary += 300;        }        static void BaoXian(ref int salary)        {            salary -= 300;        }    }

把上面的ref改為out則不行:

 class Program    {        static void Main(string[] args)        {            int basicSalary;            //ref的參數,要求傳遞進來的變數必須聲明,並且賦值。            JiangjinNum1(out basicSalary);            JiangjinNum2(out basicSalary);            BaoXian(out basicSalary);            Console.WriteLine(basicSalary);            Console.ReadKey();        }        //獎金,優秀員工獎        //out參數特點:1.傳遞到out參數中的變數,聲明不需要賦值,賦值也沒有意義        //2.在out參數所在的方法中,out參數必須賦值,並且在使用前賦值        //3.當方法中有多個傳回值得時候可以考慮out        static void JiangjinNum1(out int salary)        {            salary += 500;        }        static void JiangjinNum2(out int salary)        {            salary += 300;        }        static void BaoXian(out int salary)        {            salary -= 300;        }    }
 class Program    {        static void Main(string[] args)        {                  }        static void M1(ref int n)        {            n++;            Console.WriteLine(n);        }        static void M2(out int n)        {            n = 10;            Console.WriteLine(n);        }         }

兩個變數的交換:

    class Program    {        static void Main(string[] args)        {            int num1 = 10;            int num2 = 20;            Swap(ref num1,ref num2);            Console.WriteLine(num1);            Console.WriteLine(num2);            Console.ReadKey();        }        static void Swap(ref int n1, ref int n2)        {            int temp = n1;            n1 = n2;            n2 = temp;                }         }

out舉例

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.