C# 學習相關檔案下載參考,網址:請點我一下 。
軟件安裝 和 創建方案 / 專案
軟件安裝
官網軟件下載:https://visualstudio.microsoft.com/zh-hant/downloads/
- 選擇Visual Studio 2019 其中一個下載即可。
- C# 這邊用的是
.NET桌面開發
,如果還需要其他工作負載,可以自行選擇下載。
創建方案 / 專案
使用註釋幫助閱讀代碼
資料參考專案
Learning 01
。
- 單行註釋
1
// 單行註釋:幫助程序開發者用於閱讀代碼
- 多行註釋3.摺疊註釋
1
2
3/* 多行註釋:幫助程序開發者用於閱讀代碼
多行註釋:幫助程序開發者用於閱讀代碼
多行註釋:幫助程序開發者用於閱讀代碼 */1
2 - 文檔註釋
當滑鼠指標移動到Tset文字時,視窗註釋會顯示文檔註釋
。1
2
3
4/// <summary>
/// 文檔註釋
/// </summary>
public static void Test(){}
常用快捷鍵
- 新增新專案
在該專案裡面,按Ctrl
+
Shift
+
N
,即可跳出建立新專案
的視窗。 - 新增檔案
在該專案裡面,按Ctrl
+
N
,即可跳出建立新專案
的視窗。 - 加入新增專案項目
在該專案裡面,按Ctrl
+
Shift
+
A
,即可跳出新增項目
的視窗。 - 加入現有專案項目
在該專案裡面,按Shift
+
Alt
+
A
,即可跳出加入現有項目
的視窗。 - 區域註釋
Ctrl
+
K
+
C
選取區域註釋
Ctrl
+
K
+
U
選取區域取消註釋 - Console.WriteLine()
鍵盤輸入cw
,即可跳出Console.WriteLine();
變量理解與使用
資料參考專案
Learning 02
。
開始
資料型態值:
1 | int number; // 宣告int類別的變數 |
(方法1)一般用法:(字符加字符=拼接;數字加數字=運算)
 char print
 的print
,不能放在數字後面。
 string Type
的Type
,可放在數字後面。1
Console.WriteLine(number + Decimalpoint + " " + Type + print);
(方法2) 占位符用法:(“{0}、{1}、{2}、{3}、{4}”, 0, 1, 2, 3, 4);
1
Console.WriteLine("數字:{0} , 小數點:{1} , 字符:{2} , 字串:{3}", number, Decimalpoint, print, Type);
顯示輸出字元
1
2Console.Write("Hello World!"); // 印字
Console.WriteLine("Hello World!"); // 換行印字鍵盤輸入字元
1
2Console.Read(); // 等待輸入
Console.ReadLine("Hello World!"); // 換行輸入
變量的命名規則
資料參考專案
Learning 03
。
- Pascal 帕斯卡命名:當有多個單字組成變量或方法時,每個單字的開頭字母要大寫。
(常用處:類名、屬性、方法名、接口)1
string UserNameTest;
- 駝峰命名:多個單字出現,首單字開頭字母要小寫,其餘單字開頭字母要大寫。
1
string nameYongCheng;
- C# 變數:只能由數字,字母和下劃線組成,但是不能以數字做為變數開頭。
1
int _age1;
- 見名知意
1
string name = "黃永成";
運算符的優先級別使用
資料參考專案
Learning 04
。
- 算術運算符
+
-
*
/
%
(加、減、乘、除、餘) - 關係運算符
>
<
==
>=
<=
!=
(大於、小於、相同、大於等於、小於等於、不同) - 邏輯運算符
&&
||
!
(and閘、or閘、not閘) - 賦值運算符
=
(int number = 1;)
占位符的使用方式
資料參考專案
Learning 05
。
開始
1 | // C#占位符的使用 |
if else 判斷式的使用方式
資料參考專案
Learning 06
。
開始
情境一
假如條件是:
下雨天
有下雨的話:不出門
沒下雨的話:出門
- 代碼:
1
2
3
4
5 bool rain=true; // 布林值: true真、false假
if (rain==true)
{ Console.WriteLine("不出門"); }
else
{ Console.WriteLine("要出門"); }
情境二
假如條件是:你存款的錢有80萬元
若低於80萬元:我的女兒不能嫁給你
若高於80萬元:我的女兒才能嫁給你
- 代碼:
1
2
3
4
5
6
7
8 Console.Write("請說出你的存款金額:");
int money = int.Parse(Console.ReadLine());
// Console.ReadLine()是字串,需使用int.Parse轉成數字。
bool result = money >= 800000; // 布林值: true真、false假
if (money >= 800000)
{ Console.WriteLine("我的女兒可以嫁給你"); }
else
{ Console.WriteLine("我的女兒不可以嫁給你"); }
情境三
假如條件是:你存款的錢有80萬元,有房子,有車,沒老婆
若低於80萬元:我的女兒不能嫁給你
若高於80萬元:我的女兒才能嫁給你
- 條件宣布代碼:
1
2
3
4
5
6
7
8
9 // 條件宣布
Console.Write("請說出你的存款金額:");
int money_2 = int.Parse(Console.ReadLine());
// Console.ReadLine()是字串,需使用int.Parse轉成數字。
// 布林值: true真、false假
bool result_2 = money_2 >= 800000;
bool room = true;
bool car = true;
bool wife = false;
- 方法一:
1
2
3
4 if (room && car &&!wife && result_2)
{ Console.WriteLine("我的女兒可以嫁給你"); }
else
{ Console.WriteLine("我的女兒不可以嫁給你"); }- 方法二:
1
2
3
4
5 if (result_2 != true) { Console.WriteLine("我的女兒不可以嫁給你!"); }
else if (room != true) { Console.WriteLine("有房子再說吧~"); }
else if (car != true) { Console.WriteLine("有車再說吧~"); }
else if (wife != false) { Console.WriteLine("先離婚再說~"); }
else { Console.WriteLine("我的女兒可以嫁給你!"); }- 方法三:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29 if (result_2 == true)
{
if (room == true)
{
if (car == true)
{
if (wife == false)
{
Console.WriteLine("我的女兒可以嫁給你");
}
else
{
Console.WriteLine("我的女兒不可以嫁給你");
}
}
else
{
Console.WriteLine("我的女兒不可以嫁給你");
}
}
else
{
Console.WriteLine("我的女兒不可以嫁給你");
}
}
else
{
Console.WriteLine("我的女兒不可以嫁給你");
}
switch 和 if 的差別
資料參考專案
Learning 07
。
開始
條件:
告訴我
你的年齡
幼稚園:3~5歲
國小:6~11歲
國中:12~14歲
- 條件宣布代碼:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 // 條件宣布
Console.Write("請輸入您的年齡:");
//int age = int.Parse(Console.ReadLine());
int age;
bool good = int.TryParse(Console.ReadLine(),out age);
if (good)
{
Console.Write("您的年齡是:");
Console.WriteLine(age+"歲");
}
else
{
Console.WriteLine("你填錯了,請填數字就好");
System.Environment.Exit(0); // 終止所有程序
}
bool Kindergarten = 6 > age && age >= 3;
bool ElementarySchool = 12 > age && age >= 6;
bool secondary = 15 > age && age >= 12;
- 方法一:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27 // 方法一
Console.Write("方法一:");
switch (age) // 選擇
{
case 3:
case 4:
case 5:
Console.WriteLine("我要上幼稚園了");
break; // 跳脫
case 6:
case 7:
case 8:
case 9:
case 10:
case 11:
Console.WriteLine("我要上小學了");
break; // 跳脫
case 12:
case 13:
case 14:
Console.WriteLine("我要上國中了");
break; // case 最後一項一定要加break;
default: // 默認:沒有指定的case
Console.WriteLine("我來亂的");
break;
}- 方法二:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 // 方法二
if (Kindergarten == true) // 判斷
{
Console.Write("方法二:");
Console.WriteLine("可以上幼稚園了");
}
else if (ElementarySchool == true)
{
Console.Write("方法二:");
Console.WriteLine("可以上國小了");
}
else if (secondary == true)
{
Console.Write("方法二:");
Console.WriteLine("可以上國中了");
}
else
{
Console.Write("方法二:");
Console.WriteLine("我來亂的");
}
【練習題】C# 綜合基礎語法
資料參考專案
Learning 08
。
開始
1 | // 友善訊息 |
while 循環
資料參考專案
Learning 09
。
開始
假設使0~500,每次增值為10,直到500為止
註:【number++;】等於【number+=1;】等於【number = number + 1;】
- 方法一:
1
2
3
4
5
6
7
8
9
10
11 int number1 = 0;
while (true)
{
Console.WriteLine(number1);
if (number1 == 500)
{
Console.WriteLine(number1);
break; // 跳出while
}
number1 += 10; // number1 = number1 + 10;
}- 方法二:
1
2
3
4
5
6 int number2 = 0;
while (number2 <= 500)
{
Console.WriteLine(number2);
number2 += 10;
}
do while 循環
資料參考專案
Learning 10
。
開始
1 | int number; // 宣告區域變數 |
for 循環
資料參考專案
Learning 11
。
開始
1 | // for(變量宣告;變量條件;變量增量) |
循環運算的九九乘法表
資料參考專案
Learning 12
。
開始
九九乘法表有規可尋,所以我們可以分辨它的條件
1 | 1x1=1 |
- 代碼:
1
2
3
4
5
6
7
8 for (int x = 1; x <= 9; x += 1)
{
for (int y = 1; y <= x; y += 1)
{
Console.Write($"{y}x{x}=" + (x * y)+"\t"); // 【"\t"】段落對齊
}
Console.WriteLine();
}
數組的理解與使用
資料參考專案
Learning 13
。
開始
- 方式一:
1
2
3
4
5 string[]names = new string[3]; //宣布數組的數量,創建一組字符串內存空間
names[0] = "編號一";
names[1] = "編號二";
names[2] = "編號三";
Console.WriteLine(names[0]+"\t"+ names[1] + "\t" + names[2]);- 方式二:
1
2
3 string[] numbers = new string { "數字一", "數字二", "數字三" }; // new初始化
//string[] numbers = { "數字一", "數字二", "數字三" };
Console.WriteLine(numbers[0] + "\t" + numbers[1] + "\t" + numbers[2]);
“指定”類別的使用方式
資料參考專案
Learning 14
。
開始
類別
在該專案裡面,按Ctrl
+
Shift
+
A
,即可跳出新增項目
的視窗。
- 類別名稱建立為
Student
.cs。 class Student{}
更改為public class Student{}
- 靜態特徵:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/// <summary>
/// 學生姓名
/// </summary>
public string Name;
/// <summary>
/// 年齡
/// </summary>
public int Age;
/// <summary>
/// 性別
/// </summary>
public char Sex;
/// <summary>
/// 電話
/// </summary>
public string Phone; - 動態特徵:
1
2
3
4
5
6
7
8
9
/// <summary>
/// 學習
/// </summary>
public void Learn()
{
Console.WriteLine(this.Name + "\t" + "正在看書學習..."); // this.Name 當前名稱.姓名
}
Program 主運行程式
- 索引關聯資料建立:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17Student stu_1 = new Student();
stu_1.Name = "標號一";
stu_1.Age = 18;
stu_1.Sex = '女';
stu_1.Phone = "0987878787";
Student stu_2 = new Student();
stu_2.Name = "標號二";
stu_2.Age = 19;
stu_2.Sex = '女';
stu_2.Phone = "0978787878";
Student stu_3 = new Student();
stu_3.Name = "標號二";
stu_3.Age = 20;
stu_3.Sex = '男';
stu_3.Phone = "0969696969"; - 主要程式:
1
2
3
4
5
6
7
8Student[] stu = { stu_1, stu_2, stu_3 }; // 占位符的使用
for (int i = 0; i < stu.Length; i += 1) // stu.Length 是 stu 的長度。
{
// 靜態特徵
Console.WriteLine($"姓名:{stu[i].Name} 性別:{stu[i].Sex} 年紀:{stu[i].Age} 電話號碼:{stu[i].Phone}");
// 動態特徵
stu[i].Learn();
}
調用類別的動態特徵”執行”
資料參考專案
Learning 15
。
開始
類別
在該專案裡面,按Ctrl
+
Shift
+
A
,即可跳出新增項目
的視窗。
- 類別名稱建立為
Dog
.cs。 class Dog{}
更改為public class Dog{}
- 靜態特徵:
1
2
3
4
5
6
7
8
9
10
11
/// <summary>
/// 玩具名稱
/// </summary>
public string name;
/// <summary>
/// 玩具顏色
/// </summary>
public string color; - 動態特徵:
訪問硬件有可能會報錯,所以要設定相對應的 return 回傳值。1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32/// <summary>
/// string_Run
/// </summary>
/// <returns></returns>
public string Run_string()
{
Console.WriteLine($"{this.name}的{this.color}真難看~");
return "abc"; // 回傳值
}
/// <summary>
/// int_Run
/// </summary>
/// <returns></returns>
public int Run_int()
{
Console.WriteLine($"{this.name}的{this.color}真難看~");
return 1; // 回傳值
}
/// <summary>
/// bool_Run
/// </summary>
/// <returns></returns>
public bool Run_bool()
{
Console.WriteLine($"{this.name}的{this.color}真難看~");
return true; // 回傳值
}
Program 主運行程式
- 索引關聯資料建立:
1
2
3
4// 資料關聯建立
Dog dog1 = new Dog();
dog1.name = "醜八怪";
dog1.color = "黃色"; - 調用類別的”動態特徵”執行:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43// string
string result_string = dog1.Run_string(); // 調用類別的"動態特徵"執行
if (result_string =="abc")
{
Console.WriteLine("string調用成功");
}
else
{
Console.WriteLine("string調用失敗");
}
// int
int result_int = dog1.Run_int(); // 調用類別的"動態特徵"執行
if (result_int == 1)
{
Console.WriteLine("int調用成功");
}
else
{
Console.WriteLine("int調用失敗");
}
// bool
bool result_bool = dog1.Run_bool(); // 調用類別的"動態特徵"執行
if (result_bool == true)
{
Console.WriteLine("bool調用成功");
}
else
{
Console.WriteLine("bool調用失敗");
}
帶參數的動態特徵
資料參考專案
Learning 16
。
開始
非帶參數
類別
在該專案裡面,按Ctrl
+
Shift
+
A
,即可跳出新增項目
的視窗。
- 類別名稱建立為
Lamp
.cs。 class Lamp{}
更改為public class Lamp{}
- 代碼:
1
2
3
4
5
6
7
8
9public void Open()
{
Console.WriteLine("開燈");
}
public void Close()
{
Console.WriteLine("關燈");
}
Program 主運行程式
- 代碼:
1
2
3Lamp lamp = new Lamp();
lamp.Open();
lamp.Close();
帶參數
類別
在該專案裡面,按Ctrl
+
Shift
+
A
,即可跳出新增項目
的視窗。
- 類別名稱建立為
PPAP
.cs。 class PPAP{}
更改為public class PPAP{}
- 代碼:
1
2
3
4
5public string ppap(string fruit)
{
string result = fruit + "Pen";
return result; // 回傳值
}
Program 主運行程式
- 代碼:
1
2
3PPAP PPAP = new PPAP();
string result = PPAP.ppap("Apple");
Console.WriteLine($"{result}");
流程控制關鍵詞
資料參考專案
Learning 17
。
開始
- 流程控制關鍵詞
1
2
3break; // 終止,結束之意
continue; // 繼續,效率之意
return; // 返回,傳遞之意
範例解釋
1. break 終止
- switch
1
2
3
4
5
6
7
8
9
10
11
12 Console.Write("請輸入number值:");
int number = int.Parse(Console.ReadLine());
switch (number)
{
case 1:
Console.WriteLine("數字一");
break;
default: // 默認
Console.WriteLine("沒有東西");
break; // 終止
}- for
1
2
3
4
5
6
7
8 for (int n = 1; n < 100; n += 1)
{
if (n % 2 == 0)
{
Console.WriteLine($"第一個偶數: {n}");
break;
}
}- while
1
2
3
4
5
6
7
8
9
10 int m = 0;
while (m < 20)
{
Console.WriteLine($"m: {m}");
if (m == 10)
{
break; // 到m=10時,跳脫while迴圈。
}
m += 2;
}
2. continue 繼續
1 | int sum = 0; |
3. return 返回
1 | static void Main(string[] args) |
訪問修飾符
開始
訪問權限
private
:私有的,僅類的內部程式可以使用protected
:受保護的,類的內部和繼承子類可以訪問。internal
:內部的,在同一個專案命名空間可以訪問。public
:公共的,完全公開,沒有訪問限制。
在這裡可能無法更明確解釋,可能要實際操作才能理解。
VS調適Bug的方法
- 三種調試方運行方式
F5
:啟動運行,跳轉到下一個斷點。
斷點方式:在VS 2019中,左鍵雙擊點選每行數字的左邊空白處,有亮起紅色圓形體即可。F10
:逐步調適。F11
:逐句調適。
在這裡可能無法更明確解釋,可能要實際操作才能理解。
引用類型和值類型
資料參考專案
Learning 18
。
開始
整數型別的特性
值類型
- C# 支援下列預先定義的整數型別:
C# 型別/關鍵字 範圍 大小 .NET 類型 sbyte -128 到 127 帶正負號的 8 位元整數 System.SByte byte 0 至 255 不帶正負號的 8 位元整數 System.Byte short -32,768 至 32,767 帶正負號的 16 位元整數 System.Int16 ushort 0 到 65,535 不帶正負號的 16 位元整數 System.UInt16 int -2,147,483,648 至 2,147,483,647 帶正負號的 32 位元整數 System.Int32 uint 0 到 4,294,967,295 不帶正負號的 32 位元整數 System.UInt32 long -9,223,372,036,854,775,808 至 9,223,372,036,854,775,807 帶正負號的 64 位元整數 System.Int64 ulong 0 到 18,446,744,073,709,551,615 不帶正負號的 64 位元整數 System.UInt64
實值型別有兩種類別︰struct 和 enum。
struct
您使用 struct 關鍵字來建立您自己自訂的實值型別。 一般來說,會使用結構做為一小組相關變數的容器,如下列範例所示︰1
2
3
4
5
6
7
8
9
10public struct Coords
{
public int x, y;
public Coords(int p1, int p2)
{
x = p1;
y = p2;
}
}enum
列舉型態(或列舉類型)是由基礎整體數值類型的一組命名常量定義的值類型。 要定義的枚舉類型,請使用關鍵enum字並指定枚舉成員的名稱:
1
2
3
4
5
6
7enum Season
{
Spring,
Summer,
Autumn,
Winter
}預設情況下,枚舉成員的關聯常量值為int類型 ;它們以零開始,然後按照定義文本順序增加一個。您可以顯示式指定任何其他積分數值類型作為枚舉類型的基礎類型。 您還可以顯示式指定關聯的常量值,如以下範例所示:
1
2
3
4
5
6
7enum ErrorCode : ushort
{
None = 0,
Unknown = 1,
ConnectionLost = 100,
OutlierReading = 200
}
參考文章
- Microsoft C# 程式設計手冊( 文章連結)