標籤: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舉例