# C#基本語法
### 1.常見資料型態&變數
* 字串 string "abcd" / `string name = " abcd";`
* 字元 char 'a' / `char gender = 'M';`
* 整數 int 10 / `int age = 10;`
* 浮點數 double 160.5 / `double height = 160.5;`
* 布林值 bool true false / `bool is male = true;`
### 2.數字常見用法
* 一般運算 `System.Consle.WriteLine((5+2)*3);`
* 絕對值 `System.Consle.WriteLine(System.Math.Abs(-10));`
* 次方運算 `System.Consle.WriteLine(System.Math.Pow(2,3));`
* 開根號 `System.Consle.WriteLine(System.Math.Sqrt(16));`
* 數值大小 `System.Consle.WriteLine(System.Math.Max(2,10));`
* 四捨五入 `System.Consle.WriteLine(System.Math.Round(2.7));`
### 3.取得用戶輸入
```
System.Consle.WriteLine("請輸入你的名字:");
string name = System.Consle.ReadLine();
System.Consle.WriteLine("請輸入你的年紀:");
string age = System.Consle.ReadLine();
System.Consle.WriteLine(name+"你好,你今年"+age+"歲");
```
### 4.基本計算機
```
System.Consle.WriteLine("請輸入第一個數:");
double num1 = System.Convert.ToDouble(System.Consle.ReadLine());
System.Consle.WriteLine("請輸入第二個數:");
double num2 = System.Convert.ToDouble(System.Consle.ReadLine());
System.Consle.WriteLine(num1+num2);
```
### 5.陣列
```
int[] scores ={10,20,30,40};
string[] phones = new string[10]
//指定一個新(空)陣列可以存放幾個數值
scores[0]=20; //可指定陣列開始數值
System.Consle.WriteLine(scores[0]);
//C#中陣列排序是從0開始
System.Consle.WriteLine(scores[1]);
System.Consle.WriteLine(scores[2]);
System.Consle.WriteLine(scores[3]);
```
### 6.if判斷句
* 如果我肚子餓 我就去吃飯
```
bool hungry = true;
if(hungry){
System.Console.WriteLine("我就去吃飯");
}
```
* 如果今天下雨 我就開車去上班
**否則**我就走路去上班
```
bool rainy = true;
if(rainy){
System.Console.WriteLine("我就開車去上班");
}
else{
System.Console.WriteLine("我就走去上班");
}
```
* 如果你考100分
我給你1000元
**或是如果**你考80分以上
我給你500元
**或是如果**你考60分以上
我給你100元
**否則**
你給我300元
```
int score = 100;
if(score==100){
System.Console.WriteLine("我給你1000元");
}
else if(score >= 80){
System.Console.WriteLine("我給你500元");
}
else if(score >= 60){
System.Console.WriteLine("我給你100元");
}
else{
System.Console.WriteLine("你給我300元");
}
```
* 如果你考100分 **且** 今天有下雨
我給你1000元
否則
你給我100元
```
int score = 100;
bool rainy = true;
if(score==100 && rainy){
System.Console.WriteLine("我給你1000元")
}
else{
System.Console.WriteLine("你給我100元")
}
```
* 如果你考100分 **或** 今天有下雨
我給你1000元
否則
你給我100元
```
int score = 100;
bool rainy = true;
if(score==100 || rainy){
System.Console.WriteLine("我給你1000元");
}
else{
System.Console.WriteLine("你給我100元");
}
```
* 如果你**沒有**考100分 或 今天**沒有**下雨
我給你1000元
否則
你給我100元
```
int score = 100;
bool rainy = true;
if(score ! =100 || ! rainy){
System.Console.WriteLine("我給你1000元");
}
else{
System.Console.WriteLine("你給我100元");
}
```
### 7.進階計算機
```
System.Consle.Write("請輸入第一個數:");
double num1 = System.Convert.ToDouble(System.Consle.ReadLine());
System.Consle.Write("請輸入要做的運算");
string oper = System.Consle.ReadLine();
System.Consle.Write("請輸入第二個數:");
double num2 = System.Convert.ToDouble(System.Consle.ReadLine());
if(oper == " + "){
System.Consle.WriteLine(num1+num2);
}
else if(oper == " - "){
System.Consle.WriteLine(num1-num2);
}
else if(oper == " * "){
System.Consle.WriteLine(num1*num2);
}
else if(oper == " / "){
System.Consle.WriteLine(num1/num2);
}
else{
System.Consle.WriteLine("不合法的運算符號");
}
```
### 8.while迴圈
```
System.Consle.WriteLine(1);
System.Consle.WriteLine(2);
System.Consle.WriteLine(3);
System.Consle.WriteLine(4);
System.Consle.WriteLine(5);
int i = 1;
while(i<=5){
System.Consle.WriteLine(i);
i++;
//++寫進while裡才會執行
}
//以下do寫法會先輸出6後判斷false條件停止
int i = 6;
do //先執行{}的程式碼
{
System.Consle.WriteLine(i); //這會先印出6
i++;
//++寫進while裡才會執行
}
while(i<=5); //後判斷條件
```
### 9.猜數字遊戲
```
int s_num = 66;
int g_num;
//假設謎底數字與一個空數字供玩家輸入
do{
System.Consle.Write("請輸入一個數字:");
g_num = System.Convert.ToInt32(System.Consle.ReadLine());
if(g_num > s_num){
System.Consle.WriteLine("小一點");
}
else if(g_num < s_num){
System.Consle.WriteLine("大一點");
}
else{
System.Consle.WriteLine("bingo!");
}
}
while(g_num != s_num);
//當玩家未猜中時會一直執行do中程式碼
```
* **進階版-次數限制**
```
int s_num = 66;
int g_num;
int g_count = 0;
int g_limit = 5;
bool win = false;
//假設謎底數字與一個空數字供玩家輸入
//設定猜測次數的變數與次數限制變數
do{
System.Consle.Write("請輸入一個數字:");
g_num = System.Convert.ToInt32(System.Consle.ReadLine());
g_count++;
if(g_num > s_num){
System.Consle.WriteLine("小一點");
}
else if(g_num < s_num){
System.Consle.WriteLine("大一點");
}
else{
System.Consle.WriteLine("bingo!");
win = true;
}
}
while(g_num != s_num && g_count<g_limit );
//當玩家未猜中時會一直執行do中程式碼
if( !win ){
System.Consle.WriteLine("You lose!");
}
//如超過次數時跳出while迴圈執行的條件
```
### 10.for迴圈(用法較while簡潔)
```
int i = 1;
while(i <= 5){
System.Consle.WriteLine(i);
i++;
}
for(int i = 1; i <= 5; i++) //最後一個條件不需要;
{
System.Consle.WriteLine(i);
}
int[] nums={ 10, 20, 45, 34, 190}
for(int i = 0; i <nums.Length; i++) **// nums.Length是陣列長度**
{
System.Consle.WriteLine( nums[i] );
}
//i只要小於陣列長度就會讀取全部數值
```
### 11.二維陣列[ , , , ]
* row 橫排
* column 直排
```
int[ **,** ] nums ={
{ 1, 2, 3},
{ 4, 5, 6},
{ 7, 8, 9}
};
//用,區隔每一個陣列
int[ , ] num = new int[ 3, 4 ];
num[ 0, 0 ] = 3;
num[ 0, 1 ] = 4;
//在空的二維陣列相對位置中放入指定的值
{
System.Consle.WriteLine( nums[ 0, 0 ] );
}
```
### 12. class, object 類別, 物件(類別-物件-屬性)
**//class(類別)中定義的容器為object(物件)**
* 創建一個class類別(Person.cs)
//class(類別)中包含**資料型態**object物件,物件中又有哪些**屬性**
```
class Person
//創建一個Person的類別,只有Person資料型態模板
{
public double height;
public int age;
public string name;
}
```
* 回到program.cs
```
Person p1 = new Person();
//創建出實體Person放到p1變數容器裡儲存
//使用Person模板創出的p1為object
p1.height = 160.5;
p1.age = 18;
p1.name = "林檎";
//object中包含的資料及屬性
//可以設定一個新的p2容器,p3p4以此類推
```
### 13.namespace, using
* namespace是可以存放很多模板(class)的空間
**→用來分類跟管理模板(類似檔案夾)
→namespace裡也可以再創建namespace**
```
namespace Animal
//創建出一個叫Animal的空間
{
class Person
{
public double height;
public int age;
public string name;
}
}
//此時已將Person放入Animal
//當回到Program.cs時要使用必須寫Animal.Person
//(從namespace目錄中取用class的資料)
```
* using即為取用之意,用法為提取目錄中所有class資訊
**// System.Console.WriteLine中
→System是C#內建的namespace
→Console是class**
```
Animal.Person p1 = new Person();
p1.height = 160.5;
p1.age = 18;
p1.name = "林檎";
//可改為以下用法
using Animal;
//使用namespace裡的class
Person p1 = new Person();
p1.height = 160.5;
p1.age = 18;
p1.name = "林檎";
```
### 14. method方法
* Person.cs
```
using Animal;
using System;
class Person
{
public double height;
public int age;
public string name;
public void SayHi() //void不會回傳資料
{
Console.WriteLine("你好我是"+name);
}
}
public bool IsAdult() //bool是表資料型態
{
if(age>=18){
return true; //return是資料回傳(覆蓋),此時資料型態是布林值
//Console.WriteLine(true); 並不會將資料回傳上方,直接顯示結果
}
else{
return false;
}
}
public int Add(int num1,int num2,string QQ)
//資料型態回傳後是整數
//做兩個整數相加,呼叫時需在()中放入的int
{
return num1+num2;
}
```
**//return是將結果(值)回傳後進行後續判斷或運算,會比直接顯示結果做更多的應用**
* Program.cs
```
Person p1 = new Person();
p1.height = 160.5;
p1.age = 18;
p1.name = "林檎";
p1.SayHi();
//執行SayHi方法的程式碼
//呼叫出p1容器內屬性
p1.IsAdult();
//此法為回傳值,因此可再用於其他條件判斷
Console.WriteLine(p1.Add(2,3,"QAQ"));
//回傳後會變成5QAQ
//WriteLine()也是一種方法但型態為void並不會回傳值
//Console.WriteLine()是指WriteLine()屬於Console(內建class)下的method
```
### 15.main方法
**//程式進入點**
```
namespace ConsoleApp1
//存放class的空間
{
class Program
//類別內可存放object資料型態或呼叫方法method
{
static void Main()
//內建方法,不須呼叫執行時判斷程式進入點
{
Console.WriteLine("Hello");
}
}
}
```
### 16.Constructor建構方法
**//每創建一個物件object都會被呼叫一次的方法
→幫class新增建構方法時,public後直接class名稱()
→建構方法不能回傳資料,前不需寫上回傳資料型態(如void,int等)**
```
namespace ConsoleApp1 {
class Person
{
public double height;
public int age;
public string name;
public Person(double h,int a,string n)
//建構方法,每創建一Person物件就執行一次的程式碼
//Person()不會回傳資料,前不需寫上回傳資料型態
//Person()內可接受資料
{
this.height = height;
this.age = age;
this.name = name;
//this為屬性之意,指傳進來的值
}
}
static void Main() //程式進入點
{
Person p1 = new Person(160,18,"林檎");
Person p2 = new Person(180,22,"林果");
Console.WriteLine(p1.name);
Console.WriteLine(p1.name);
}
}
```
### 17.getter,setter
**//可用來限制屬性存取,第一步把public改private
→public的屬性可在任何地方被存取,private限制在該class
//getter,setter是幫助執行存取回傳限制屬性**
* Video.cs
```
class Video
{
public string title;
public string author;
private string type;
//影片類型只設定4種:少年 熱血 冒險 其他
//屬性type只能在class video中使用
//限制屬性如要在該class以外取用需透過getter,setter
public Video(string title,string author,string type)
{
this.title=title;
this.author=author;
Type=type;
//this.type=type;的寫法無法用getter,setter讓其他地方存取
//在class Video以外的地方要存取type值(限制屬性)需透過Type
}
//getter,setter用法
public string Type{
//以限制的type屬性的資料型態(string)寫上並命名Type
//Type相當於type的對外代理人
get{return type;}
//當要取得Type時執行的指令
//這邊做回傳原先設定的type屬性
set {
//設定Type時要做的事
if(value=="少年"||value=="熱血"||value=="冒險"||value=="其他")
{
type=value;
//設定value為Type的值(正確設定時)
}
else
{
type="其他";
//如出現設定屬性以外屬性時一律顯示"其他"
}
}
}
```
* Program.cs
**//無法存取type屬性,但可透過Type取得**
```
using ConsoleApp1;
using System;
Video video1=new Video("獵人","富堅","庫拉皮卡要下船");
Video video1=new Video("海賊王","尾田","熱血");
Console.WriteLine(video1.Type);
//在class Video外存取type使用Type
//→輸出時本應顯示"庫拉皮卡要下船",經Type裡else判定會顯示"其他"
```
18.static attritube靜態屬性
**//static的屬性是類別class本身的**
* Video.cs
```
class Video
{
public string title;
public string author;
public string type;
//class Video中創造出的物件包含三個屬性(title,author,type)
public static int video_c=0;
//屬於class Video
public Video(string title,string author,string type)
{
this.title=title;
this.author=author;
this.type=type;
video_c++; //每創建一個video物件video_c的值會增加1
}
public int getVideoCount()
{
return video_c;
}
}
```
* Program.cs
```
using ConsoleApp1;
using System;
Video video1=new Video("獵人","富堅","少年");
Video video2=new Video("海賊王","尾田","熱血");
//video1,video2為物件(static屬性不通用)
Console.WriteLine(Video.video_c);
//從class Video中取得video_c的值
//→創造了2個video物件所以值為2
Console.WriteLine(video2.Type);
Console.WriteLine(video1.getVideoCount());
```
### 19.static method靜態方法 ,static class靜態類別
**//創建方法即在前加上static
//static method是class本身的方法,static class不需要創建物件**
* Program.cs
```
using ConsoleApp1;
using System;
Tool.SayHi();
Console.WriteLine(Math.Sqrt(36));
//使用Sqrt方法,不須創建Math即可使用(Math是一種工具包)
//Sqrt即為一種static method
```
* Tool.cs
**//創建static method**
```
namespace ConsoleApp1
{
static class Tool{
public static void SayHi()
{
Console.WriteLine("Hello");
}
}
}
```
### 20.inheritance繼承
//(子)小類別可繼承(父)大類別(概括同一屬性)
//→class 子類別 : 父類別
* Program.cs
```
using ConsoleApp1;
using System;
Stuent stuent1=new Stuent("林檎",18,"桃園高中")
Console.WriteLine(student1.age);
//已繼承Person屬性故可輸出18
student1.PrintName();
//已繼承Person屬性故可輸出"林檎"
```
* Person.cs
```
class Person //創建class Person包含2個屬性
{
public string name;
public int age;
public void PrintAge() //方法一輸出年紀
{
Console.WriteLine(this.age);
}
public void PrintName() //方法二輸出名字
{
Console.WriteLine(this.name);
}
}
```
* Student.cs
```
class Student : Person
//繼承方式為→ 該類別 : 要繼承的父類別
//創建class Student包含3個屬性
{
//public string name;
//public int age;
//繼承後即可使用Person中已有屬性,故可不寫
public string school;
public Student(string name,int age,string school)
{
this.name=name;
this.age=age;
this.school=school;
}
//寫出一個建構方法,每創建一個物件時即呼叫
/*public void PrintAge()
{
Console.WriteLine(this.age);
}
public void PrintName()
{
Console.WriteLine(this.name);
}*/
//繼承後即可使用Person中已有方法,故可不寫
public void PrintSchool()
{
Console.WriteLine(this.school);
}
}
```