-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathProgram.cs
More file actions
298 lines (254 loc) · 7.31 KB
/
Program.cs
File metadata and controls
298 lines (254 loc) · 7.31 KB
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProgrammaticFlow
{
class Program
{
static void Main(string[] args)
{
ShowThrow();
}
private static void ShowThrow()
{
if (true)
{
// 整個應用程式會停掉,且拋出Exception訊息
throw new Exception();
}
}
private static void ShowReutrn()
{
int i = 1;
if (i == 1)
{
// 直接跳出整個method
return;
}
//以下程式碼執行不到
DoSomething();
}
private static void ShowGotoSwitch()
{
// switch 用法
int index = 1;
int i = 1;
switch (i)
{
case 0 :
// index ++ => index = 4;
index++;
// 結束
break;
case 1:
// index += 2 => index = 3;
index += 2;
// 跳到case 0 程式碼執行
goto case 0;
case 2:
index += 4;
break;
}
Console.WriteLine(index); // output : 4
}
private static void ShowGotoLabel()
{
for (int i = 0; i < 10; i++)
{
Console.WriteLine(i);
if (i == 5)
{
goto GoHere;
}
}
// DoSomething 不會執行到
DoSomething();
GoHere:
Console.WriteLine("使用GoTo");
}
private static void ShowContinue()
{
List<string> list = new List<string>()
{
"Kevin",
"Bob",
"Miles"
};
foreach (var item in list)
{
if (item.Equals("Bob"))
{
// 如果迴圈跑到Bob,則不會執行之後的程式碼,且直接進入下一個迴圈
continue;
}
Console.WriteLine(item);
}
// output : Kevin
// output : Miles
}
private static void ShowBreak()
{
List<string> list = new List<string>()
{
"Kevin",
"Bob",
"Miles"
};
foreach (var item in list)
{
Console.WriteLine(item);
if (item.Equals("Bob"))
{
// 如果迴圈跑到Bob,則會跳出整個foreach
break;
}
}
// output : Kevin
// output : Bob
}
private static void ShowFor()
{
var value = new[] {"Kevin", "Bob", "Smith"};
// length = 3;
int length = value.Length;
// i 從 0 開始算,如果 i < 3 ,則跑一次迴圈後, i++
for (int i = 0; i < length; i++)
{
Console.WriteLine(value[i]);
// output: Kevin
// output: Bob
// output: Smith
}
}
private static void ShowForeach()
{
List<int> list = new List<int>()
{
1,2,3,4,5
};
// 取出所有元素,每個元素跑一次迴圈
foreach (var item in list)
{
Console.WriteLine(item);
// output : 1
// output : 2
// output : 3
// output : 4
// output : 5
}
}
private void ShowIf()
{
int i = 1;
// 兩個等號,代表相等
if (i == 1)
{
DoSomething();
}
// != 代表不相等
if (i != 1)
{
DoSomething();
}
// 連再一起混用
if (i == 1)
{
DoSomething();
}
else if (i == 2)
{
DoSomething();
}
else
{
DoSomething();
}
}
private void ShowSwitch()
{
int i = 3;
switch (i)
{
case 1:
// 如果 i == 1 則執行這段
DoSomething();
// 使用break 跳出switch
break;
case 2:
case 3:
// 如果 i == 2 或者 i ==3 執行這段
DoSomething();
// 使用break 跳出switch
break;
default:
// 如果 i 不等於上敘條件,則執行這段
break;
}
}
private static void ShowIfTernary()
{
int i = 1;
string message1;
// 一般if 寫法
if (i == 1)
{
message1 = "成立";
}
else
{
message1 = "不成立";
}
Console.WriteLine(message1); // output : 成立
// 可以將上面if判斷,改寫成if 三元運算寫法,讓程式碼更容易閱讀
string message2 = i == 1 ? "成立" : "不成立";
Console.WriteLine(message2); // output : 成立
// 判斷null 的用法
string value1 = null;
string message3;
if (value1 == null)
{
message3 = "是null";
}
else
{
// 如果非null,則將value1的值傳給message3
message3 = value1;
}
Console.WriteLine(message3);
// 三元運算判斷null的用法
string value2 = null;
// 使用兩個??表示判斷是否為null
// 如果是null 則 assign "是null" 字串給 message4
// 如果不是null 則 message4 = value2
var message4 = value2 ?? "是null"; // 結果與上敘述判斷null是一樣的
Console.WriteLine(message4);
}
private static void ShowWhile()
{
// do while
int index = 1;
// 會先跑一次迴圈,在判斷index 是否小於 10
do
{
// 至少執行一次或多次
Console.WriteLine(index);
index++;
} while (index < 10);
// while
int index2 = 1;
// 會先判斷index 是否小於 10 ,再跑迴圈
while (index2 < 10)
{
// 可能會沒有執行 或 執行多次
Console.WriteLine(index2);
index2++;
}
}
private static void DoSomething()
{
throw new NotImplementedException();
}
}
}