# C#🧠 資料類型與變數
> What? `變數`
```csharp
int age = 10;
```
# 資料類型 (DATA Types)
> 整數類別
| 型別 | 範圍 | 範例 |
| ------- | ------------------------------ | --------------------------------- |
| `int` | -2,147,483,648 到 2,147,483,647 | `int x = 10;` |
| `long` | 非常大的整數 | `long bigNumber = 1234567890123;` |
| `short` | -32,768 到 32,767 | `short s = 30000;` |
| `byte` | 0 到 255 | `byte b = 255;` |
> 浮點數
| 型別 | 精度 | 範例 |
| --------- | ---------------- | -------------------------- |
| `float` | 約 6\~9 位小數 | `float pi = 3.14f;` |
| `double` | 約 15\~17 位小數(預設) | `double g = 9.8;` |
| `decimal` | 精度高,用於金錢運算 | `decimal money = 100.50m;` |
> 字元字串
| 型別 | 說明 | 範例 |
| -------- | ---- | --------------------- |
| `char` | 單一字元 | `char letter = 'A';` |
| `string` | 字串 | `string name = "C#";` |
> 布林值
| 型別 | 說明 | 範例 |
| ------ | ----------------- | ----------------------- |
| `bool` | 布林值(true 或 false) | `bool isPassed = true;` |
# 🧪變數宣告與初始化
```csharp
int score = 90; // 宣告 int 並初始化
string name = "Sanxian"; // 宣告 string 並初始化
bool isOK = false; // 宣告 bool 並初始化
```
> 也可以先宣告再給予數值
```csharp
int age =;
age = 17 ;
```
# 🧮型別轉換
> 顯示轉換
```csharp
double pi = 3.14;
int i (int)d; //強制轉型,i = 3
```
> 隱式轉換
```csharp
int x = 100;
double y = x;//自動轉型double
```
# 📦`var`關鍵字
> 可以根據數值自動推斷型別(run時決定)
```csharp
var num = 10;//推斷int
var name = "Sanxian";//推斷string
```
# 運算子與運算式
> 運算式是一種運算子與運算元所組成的表示式,其結果會產生值。
```csharp
int result = 5 + 3 * 2;
```
> 上面是運算式,最終result的值會等於 11
# 運算子(Operators)
> ➕算數運算子
| 運算子 | 說明 | 範例 |
| --- | ------- | ------- |
| `+` | 加法 | `a + b` |
| `-` | 減法 | `a - b` |
| `*` | 乘法 | `a * b` |
| `/` | 除法 | `a / b` |
| `%` | 餘數(mod) | `a % b` |
```csharp
int a = 10, b = 3;
Console.WriteLine(a / b);
Console.WriteLine(a % b);
```
> ➕➖單元運算子(Unar資Operators)
| 運算子 | 說明 | 範例 |
| ---- | ------- | ------------- |
| `+` | 正號 | `+a` |
| `-` | 負號 | `-a` |
| `++` | 遞增(前/後) | `++a` 或 `a++` |
| `--` | 遞減(前/後) | `--a` 或 `a--` |
```csharp
int x = 5;
Console.WriteLine(++x);
Console.WriteLineA(x++);
```
> 🆚比較運算子(Relational Operators)
| 運算子 | 說明 | 範例 |
| ---- | ---- | -------- |
| `==` | 相等 | `a == b` |
| `!=` | 不相等 | `a != b` |
| `>` | 大於 | `a > b` |
| `<` | 小於 | `a < b` |
| `>=` | 大於等於 | `a >= b` |
| `<=` | 小於等於 | `a <= b` |
```csharp
bool result = (5 != 3)//true
```
> ☁️邏輯運算子(Lobical Operators)
| 運算子 | 說明 | 範例 | | | | |
| ---- | ------ | -------- | ----- | --- | - | --- |
| `&&` | 邏輯 AND | `a && b` | | | | |
| \` | | \` | 邏輯 OR | \`a | | b\` |
| `!` | 邏輯 NOT | `!a` | | | | |
```csharp
bool a = true, b = false;
Console.WriteLine(a && b);
```
> 📔指派運算子(Assignment Operators)
| 運算子 | 說明 | 範例 |
| ---- | ----- | -------- |
| `=` | 指派 | `a = 5` |
| `+=` | 加後指派 | `a += 3` |
| `-=` | 減後指派 | `a -= 2` |
| `*=` | 乘後指派 | `a *= 4` |
| `/=` | 除後指派 | `a /= 2` |
| `%=` | 餘數後指派 | `a %= 3` |
```csharp
int a = 5;
a += 2;// a=7
```
> 條件運算子(Ternary Operator)
```csharp
int age = 18;
string result = (age >= 18)? "成年人" : "未成年";
```
> 位元運算子(Bitwise Operators)
| 運算子 | 說明 | 範例 | | |
| ---- | ------ | -------- | --- | --- |
| `&` | 位元 AND | `a & b` | | |
| \` | \` | 位元 OR | \`a | b\` |
| `^` | 位元 XOR | `a ^ b` | | |
| `~` | 位元 NOT | `~a` | | |
| `<<` | 左移 | `a << 1` | | |
| `>>` | 右移 | `a >> 1` | | |
# 運算子優先順序(常見)
從高到低(同優先順序左至右):
1.++ --(前置)、+ -(正負)
2. * / %
3. + -
4. 比較運算子:< > <= >=
5. 相等運算子:== !=
6. 邏輯運算子:&& ||
7. 指派運算子:=, +=, -=, *=, /=, %=
8.三元運算子:? :
# 🧠 C# 輸入方法總整理(含轉型差異)
---
## ✅ 一、最常用的輸入:`Console.ReadLine()`
```csharp
Console.Write("請輸入姓名:");
string name = Console.ReadLine();
```
---
## ➕ 搭配轉型為整數:
```cs
int number = int.Parse(Console.ReadLine());
```
---
## ✅ 二、安全轉型:int.TryParse()
```cs
Console.Write("請輸入整數:");
string input = Console.ReadLine();
if (int.TryParse(input, out int num)) {
Console.WriteLine("成功輸入:" + num);
} else {
Console.WriteLine("輸入錯誤!");
}
```
---
## ✅ 三、進階轉型:Convert.ToInt32()
```cs
Console.Write("請輸入整數:");
string input = Console.ReadLine();
int number = Convert.ToInt32(input);
```
---
| 方法 | 輸入型別 | 錯誤處理 | 說明 |
| ------------------- | ----- | ---------------- | ----------- |
| `int.Parse()` | 僅限字串 | 格式錯誤會拋例外 | 最快,但易出錯 |
| `Convert.ToInt32()` | 可接多型別 | `null` 變 0;錯誤拋例外 | 較通用 |
| `int.TryParse()` | 僅限字串 | ✅ 不會拋例外 | 最安全,用於使用者輸入 |
---
## ✅ 四、命令列參數輸入:args[]
```cs
static void Main(string[] args) {
Console.WriteLine("參數:" + args[0]);
}
```
---
執行方式
```
dotnet run Hello
```
---
## ✅ 五、從檔案輸入
```cs
string[] lines = File.ReadAllLines("data.txt");
foreach (string line in lines) {
Console.WriteLine("讀入:" + line);
}
```
---
## 📄 使用 StreamReader(逐行讀取)
```cs
using (StreamReader sr = new StreamReader("data.txt")) {
string line;
while ((line = sr.ReadLine()) != null) {
Console.WriteLine("讀到:" + line);
}
}
```
---
## ✅ 六、GUI 輸入(WinForms / WPF)
```cs
string input = textBox1.Text;
int num = int.Parse(textBox2.Text);
```
---
## 📌 常見輸入方法總表
| 方法 | 用途 | 備註 |
| --------------------- | --------- | -------------- |
| `Console.ReadLine()` | 終端機輸入 | 最常用 |
| `int.Parse()` | 字串轉整數 | 輸入錯誤會當掉 |
| `Convert.ToInt32()` | 多型別轉整數 | `null` 會變 0 |
| `int.TryParse()` | 安全轉整數 | ✅ 不會崩潰,適合使用者輸入 |
| `args[]` | 命令列參數 | 執行時傳參數 |
| `File.ReadAllLines()` | 檔案輸入(每行) | 回傳字串陣列 |
| `StreamReader` | 逐行讀入大型檔案 | 效率高 |
| `textBox.Text` | GUI 使用者輸入 | WinForm/WPF 常用 |
---
### 📚 建議:初學者多用 Console.ReadLine() 搭配 TryParse(),進階再嘗試從檔案、GUI、args[] 等方式讀入資料。
---
{"title":"C#學習筆記","description":"int number = int.Parse(Console.ReadLine());","contributors":"[{\"id\":\"2f62daf7-9f3a-4b67-93f9-740305d27bab\",\"add\":6251,\"del\":55,\"latestUpdatedAt\":1752207775136}]"}