Sanxian
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights New
    • Engagement control
    • Make a copy
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Note Insights Versions and GitHub Sync Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Make a copy Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       Owned this note    Owned this note      
    Published Linked with GitHub
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    # 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[] 等方式讀入資料。 ---

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully