# C# ###### tags : `暑假` ## 環境建置 - Visual Studio 2022 - .NET桌面開發 -------- ## code ### 輸入 - ```System.Console.ReadLine();``` : - int : ```int num = System.Convert.ToInt32(System.Console.ReadLine());``` (超麻煩) - char : ```char x = (char)Console.Read();``` - string : ```string name = System.Console.ReadLine();``` ### 輸出 - ```System.Console.WriteLine``` : (輸出且換行) ```System.Console.Write``` : (輸出但不換行) - int : ```System.Console.WriteLine(123);``` - char : ```System.Console.WriteLine('a');``` - string : ```System.Console.WriteLine("Hello World");``` ### string - string 函式(string x = "riku", string y = "weized") - ```x.Length``` : 輸出字串長度 - ```x.ToUpper()``` : 轉大寫 - ```x.ToLower()``` : 轉小寫 - ```x.Contains(y)``` : 判斷x裡有沒有y,會回傳bool值 - ```x[0]``` : 回傳x第0位置字元(即r) - ```x.IndexOf('char'/"string")``` : 尋找char/string在x的第幾位,若找不到則回傳-1 - ```x.Substring(0, 2)``` : 將x第0個字元前(包含)的字元刪除,保留第0個字元後的2個字元(即ik) ### num - num運算 - ```System.Console.WriteLine(5 +-*/ 2)``` : 四則運算 - ```System.Math.Abs(-10)``` : 絕對值 - ```System.Math.Pow(2, 2)``` : 次方 - ```System.Math.Sqrt(36)``` : 開根號 - ```System.Math.Max(36, 2)``` : 找最大 - ```System.Math.Min(36, 2)``` : 找最小 - ```System.Math.Round(36.3)``` : 四捨五入 ### Array - array宣告 - ```int[] scores = {50, 60, 70, 30, 20, 90};``` - ```string[] name = new string[10];```(10為陣列大小) - ``` ///二維陣列 int[,] nums = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} };///nums[0,0] == 1 int[,] num = new int[3,4](3, 4為陣列大小) ``` ### if判斷式 ``` if(condition) { .... } else if(condition) { .... } else { .... } ``` ### while ``` while(condition) { ... } ``` ### for ``` for(int i = 0; i < 10; i++) { ... } ``` ### class - 創建class ![](https://i.imgur.com/fM71mAi.png) (さかまた:)) ![](https://i.imgur.com/L5Glbiw.png) ``` class Person { public double height; public int age; public string name; } ``` ``` Person person1 = new Person(); ///person1是一個object person1.height = 170.5; preson1.age = 44; ``` - namespace(class 空間) - 可以將class分類整理好 ``` ///class.cs namespace Animal { class Ant { public double height; public int age; public string name; } class Elephant { ... } ... } ``` ``` ///main.cs using Animal; ///不加分號的話,必須在class名稱前加上namespace的名稱 ///例えば : Animal.Ant.ant1 = new Animal.Ant();  Ant ant1 = new Ant(); ant1.height = 178.6; ant1.age = 20; ``` :::info System.Console.WriteLine 也可以忽略System,但要加上using System (自己試過好像不用加using System也行) ::: - class 函式 ``` /// class.cs class Ant { public double height; public int age; public string name; public void sayname() { Console.WriteLine("I'm " + name); } } ``` ``` ///main.cs Ant ant1 = new Ant(); ant1.name = "riku"; ant1.height = 178.6; ant1.age = 20; ant1.sayname(); /// output : I'm riku ``` - constructor(可將多個同類型的彙整起來, main看起來比較乾淨) ``` /// class.cs class Person { public double height; public int age; public string name; public Person(double height, int age, string name) { this.height = height; this.age = age; this.name = name; } } ``` ``` ///main.cs Person person1 = new Person(178.6, 20, "riku"); Person person2 = new Person(5, 3, "fff"); Console.WriteLine(person1.height); ... ``` - getter setter ``` ///class.cs class Video { public string title; public string author; private string type; public Video(string title, string author, string type) { this.title = title; this.author = author; Type = type; } public string Type { get { return type; } set { if (value == "教育" || value == "娛樂" || value == "音樂") type = value; else type = "其他"; } } } ``` ``` ///main.cs Video video1 = new Video("他", "我", "2"); Console.WriteLine(video1.Type); ///output : 其他 ``` 這裡是將class裡的type設為private,限制獲取type的地方,若要在class以外的地方獲取type則需要使用get(所以main裡只能呼叫viedo1.Type) 若沒有get函式,則無法獲取type,且這個狀態下的type是only write ``` ///main.cs Video video1 = new Video("他", "我", "教育"); video1.Type = "5"; Console.WriteLine(video1.Type); ///output : 其他 ``` 若沒有set函式,則無法再額外賦值,也就是這個狀態下的type是only read, 而set也可以將值限制在自己需要的範圍內