<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                # C# 變量和(原始)數據類型 > 原文: [https://www.programiz.com/csharp-programming/variables-primitive-data-types](https://www.programiz.com/csharp-programming/variables-primitive-data-types) #### 在本教程中,我們將學習變量,如何在 C# 中創建變量以及 C# 編程語言支持的不同數據類型。 變量是賦予存儲位置的符號名稱。 變量用于將數據存儲在計算機程序中。 * * * ## 如何在 C# 中聲明變量? 這是在 C# 中聲明變量的示例。 ```cs int age; ``` 在此示例中,聲明了類型為`int`(整數)的變量`age`,并且它只能存儲整數值。 我們可以稍后在程序中為變量分配一個值,如下所示: ```cs int age; ... ... ... age = 24; ``` 但是,在聲明過程中也可以將變量初始化為某個值。 例如, ```cs int age = 24; ``` 在此,聲明類型為`int`的變量`age`并將其初始化為`24`。 由于這是一個變量,因此我們也可以更改變量的值。 例如, ```cs int age = 24; age = 35; ``` 這里,`age`的值從 24 更改為 35。 * * * 必須先聲明 C# 中的變量,然后才能使用它們。 這意味著,必須先知道變量的名稱和類型,然后才能為其分配值。 這就是為什么 C# 被稱為[靜態類型語言](https://stackoverflow.com/questions/1517582/what-is-the-difference-between-statically-typed-and-dynamically-typed-languages "Statically typed language")的原因。 聲明后,就不能在范圍內更改變量的數據類型。 范圍可以看作是代碼塊,其中變量可見或可以使用。 如果您不理解前面的說明,請不要擔心,我們將在后面的章節中了解范圍。 現在請記住,我們無法在 C# 中執行以下操作: ```cs int age; age = 24; ... ... ... float age; ``` * * * ### 隱式變量 另外,在 C# 中,我們可以使用`var`關鍵字聲明變量而無需知道其類型。 此類變量稱為**隱式類型局部變量**。 使用`var`關鍵字聲明的變量必須在聲明時進行初始化。 ```cs var value = 5; ``` 編譯器根據分配給變量的值來確定變量的類型。 在上面的示例中,`value`的類型為`int`。 這等效于: ```cs int value; value = 5; ``` 您可以了解有關[隱式類型局部變量](https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/implicitly-typed-local-variables)的更多信息。 * * * ## C# 中的變量命名規則 命名變量時,我們需要遵循某些規則。 在 C# 中命名變量的規則是: 1. 變量名稱只能包含字母(大寫和小寫),下劃線(_)和數字。 2. The variable name must start with either letter, underscore or @ symbol. For example, Rules for naming variables in C# | 變量名 | 備注 | | --- | --- | | `name` | 有效 | | `subject101` | 有效 | | `_age` | 有效(命名私有成員變量的最佳實踐) | | `@break` | 有效(如果名稱是保留關鍵字,則使用) | | `101subject` | 無效(以數字開頭) | | `your_name` | 有效 | | `your name` | 無效(包含空格) | 3. C# 區分大小寫。 這意味著`age`和`Age`涉及 2 個不同的變量。 4. 變量名稱不能是 C# 關鍵字。 例如,`if`,`for`,`using`不能是變量名。 在下一個教程中,我們將討論 [C# 關鍵字](/csharp-programming/keywords-identifiers "C# Keywords and Identifiers")的更多信息。 * * * ## 命名變量的最佳做法 1. 選擇一個有意義的變量名。 例如,`name`,`age`,`subject`比`n`,`a`和`s`更有意義。 2. 使用 **camelCase** 表示法(以小寫字母開頭)來命名局部變量。 例如,`numOfStudents`,`age`等。 3. 使用 **PascalCase** 或 **CamelCase** (以大寫字母開頭)來命名公共成員變量。 例如,`Name`,`Price`等。 4. 使用前導下劃線(`_`)后跟 **camelCase** 表示法來命名私有成員變量。 例如,`_bankBalance`,`_emailAddress`等。 您可以在處了解有關 C# 中[命名約定的更多信息](https://softwareengineering.stackexchange.com/questions/209532/naming-convention-of-variables-in-c-programming-language)。 不用擔心公共和私有成員變量。 我們將在后面的章節中了解它們。 * * * ## C# 基本數據類型 C# 中的變量大致分為兩種類型**:值類型**和**引用類型**。 在本教程中,我們將討論作為值類型的子類的原始(簡單)數據類型。 引用類型將在以后的教程中介紹。 但是,如果您想了解更多有關變量類型的信息,請訪問 [C# 類型和變量](https://docs.microsoft.com/en-us/dotnet/csharp/tour-of-csharp/types-and-variables)(官方 C# 文檔)。 ### `bool`(布爾) * 布爾數據類型具有兩個可能的值:`true`或`false` * **默認值**:`false` * 布爾變量通常用于檢查條件,例如,*循環*等*中的條件。* 例如: ```cs using System; namespace DataType { class BooleanExample { public static void Main(string[] args) { bool isValid = true; Console.WriteLine(isValid); } } } ``` 當我們運行程序時,輸出將是: ```cs True ``` * * * ### 帶符號整數 這些數據類型保存整數值(正數和負數)。 在所有可用位中,一位用于符號。 **1.`sbyte`** * **大小**:8 位 * **范圍**:-128 至 127。 * **默認值**:0 例如: ```cs using System; namespace DataType { class SByteExample { public static void Main(string[] args) { sbyte level = 23; Console.WriteLine(level); } } } ``` 當我們運行程序時,輸出將是: ```cs 23 ``` 嘗試分配超出范圍的值,即小于 -128 或大于 127,然后看看會發生什么。 * * * **2.`short`** * **大小**:16 位 * **范圍**:-32,768 至 32,767 * **默認值**:0 例如: ```cs using System; namespace DataType { class ShortExample { public static void Main(string[] args) { short value = -1109; Console.WriteLine(value); } } } ``` 當我們運行程序時,輸出將是: ```cs -1109 ``` * * * **3\. `int`** * **大小**:32 位 * **范圍**:-231 至 231-1 * **默認值**:0 例如: ```cs using System; namespace DataType { class IntExample { public static void Main(string[] args) { int score = 51092; Console.WriteLine(score); } } } ``` 當我們運行程序時,輸出將是: ```cs 51092 ``` * * * **4.`long`** * **大小**:64 位 * **范圍**:-263 至 263-1 * **默認值**:`0L`【末尾的`L`表示該值是`long`型】 例如: ```cs using System; namespace DataType { class LongExample { public static void Main(string[] args) { long range = -7091821871L; Console.WriteLine(range); } } } ``` 當我們運行程序時,輸出將是: ```cs -7091821871 ``` * * * ### 無符號整數 這些數據類型僅保留等于或大于 0 的值。當我們確定不會有負值時,通常使用這些數據類型來存儲值。 **1.`byte`** * **大小**:8 位 * **范圍**:0 到 255。 * **默認值**:0 例如: ```cs using System; namespace DataType { class ByteExample { public static void Main(string[] args) { byte age = 62; Console.WriteLine(level); } } } ``` 當我們運行程序時,輸出將是: ```cs 62 ``` * * * **2\. `ushort`** * **大小**:16 位 * **范圍**:0 到 65,535 * **默認值**:0 例如: ```cs using System; namespace DataType { class UShortExample { public static void Main(string[] args) { ushort value = 42019; Console.WriteLine(value); } } } ``` 當我們運行程序時,輸出將是: ```cs 42019 ``` * * * **3\. `uint`** * **大小**:32 位 * **范圍**:0 到 232-1 * **默認值**:0 例如: ```cs using System; namespace DataType { class UIntExample { public static void Main(string[] args) { uint totalScore = 1151092; Console.WriteLine(totalScore); } } } ``` 當我們運行程序時,輸出將是: ```cs 1151092 ``` * * * **4\. `ulong`** * **大小**:64 位 * **范圍**:0 到 264-1 * **默認值**:0 例如: ```cs using System; namespace DataType { class ULongExample { public static void Main(string[] args) { ulong range = 17091821871L; Console.WriteLine(range); } } } ``` 當我們運行程序時,輸出將是: ```cs 17091821871 ``` * * * ### 浮點 這些數據類型保存浮點值,即包含十進制值的數字。 例如 12.36,-92.17 等。 **1.`float`** * 單精度浮點型 * **大小**:32 位 * **范圍**:`1.5×10^?45`至`3.4×10^38` * **默認值**:`0.0F`【`F`表示值是浮點型】 例如: ```cs using System; namespace DataType { class FloatExample { public static void Main(string[] args) { float number = 43.27F; Console.WriteLine(number); } } } ``` 當我們運行程序時,輸出將是: ```cs 43.27 ``` * * * **2.`double`** * 雙精度浮點類型。 [單精度和雙精度浮點有什么區別?](https://stackoverflow.com/questions/801117/whats-the-difference-between-a-single-precision-and-double-precision-floating-p) * **大小**:64 位 * **范圍**:`5.0×10^?324`至`1.7×10^308` * **默認值**:`0.0D`【`D`表示值是雙精度型】 例如: ```cs using System; namespace DataType { class DoubleExample { public static void Main(string[] args) { double value = -11092.53D; Console.WriteLine(value); } } } ``` 當我們運行程序時,輸出將是: ```cs -11092.53 ``` * * * ### `char`(字符) * 它代表一個 16 位 unicode 字符。 * **大小**:16 位 * **默認值**:`"\0"` * **范圍**:U+0000(`'\u0000'`)至 U+FFFF(`'\uffff'`) 例如: ```cs using System; namespace DataType { class CharExample { public static void Main(string[] args) { char ch1 ='\u0042'; char ch2 = 'x'; Console.WriteLine(ch1); Console.WriteLine(ch2); } } } ``` 當我們運行程序時,輸出將是: ```cs B x ``` `'B'`的 unicode 值為`'\u0042'`,因此打印`ch1`將打印`'B'`。 * * * ### `decimal` * 與浮點類型(雙精度和浮點型)相比,十進制類型具有更高的精度和更小的范圍。 因此,它適合進行貨幣計算。 * **大小**:128 位 * **默認值**:`0.0M`【`M`表示十進制類型的值】 * **范圍**:(`-7.9x10^28`至`7.9x10^28`)/(100 至 28) 例如: ```cs using System; namespace DataType { class DecimalExample { public static void Main(string[] args) { decimal bankBalance = 53005.25M; Console.WriteLine(bankBalance); } } } ``` 當我們運行程序時,輸出將是: ```cs 53005.25 ``` 必須在末尾添加后綴`M`或`m`,否則該值將被視為雙精度值,并會產生錯誤。 * * * ## C# 字面值 讓我們看下面的語句: ```cs int number = 41; ``` 這里, * `int`是一種數據類型 * `number`是變量, * `41`是字面值 字面值是程序中顯示的固定值。 他們不需要任何計算。 例如,`5`,`false`,`'w'`是直接出現在程序中的字面值,無需任何計算。 * * * ### 布爾字面值 * `true`和`false`是可用的布爾字面值。 * 它們用于初始化布爾變量。 例如: ```cs bool isValid = true; bool isPresent = false; ``` * * * ### 整數字面值 * 整數字面值用于初始化整數數據類型的變量,即`sbyte`,`short`,`int`,`long`,`byte`,`ushort`,`uint`和`ulong`。 * 如果整數字面值以`L`或`l`結尾,則其類型為`long`。 為了獲得最佳實踐,請使用`L`(而非`l`)。 ```cs long value1 = 4200910L; long value2 = -10928190L; ``` * 如果整數字面值以`0x`開頭,則它表示十六進制值。 沒有前綴的數字將被視為十進制值。 在 C# 中不允許使用八進制和二進制表示形式。 ```cs int decimalValue = 25; int hexValue = 0x11c;// decimal value 284 ``` * * * ### 浮點字面值 * 浮點字面值用于初始化`float`和`double`數據類型的變量。 * 如果浮點字面值以后綴`f`或`F`結尾,則其類型為`float`。 同樣,如果以`d`或`D`結尾,則其類型為`double`。 如果兩個后綴都不存在,則默認**類型**為`double`。 * 這些字面值以科學計數法表示時包含`e`或`E`。 ```cs double number = 24.67;// double by default float value = -12.29F; double scientificNotation = 6.21e2;// equivalent to 6.21 x 102 i.e. 621 ``` * * * ### 字符和字符串字面值 * 字符字面值用于初始化`char`數據類型的變量。 * 字符字面值用單引號引起來。 例如`'x'`,`'p'`等。 * 它們可以表示為字符,十六進制轉義序列,unicode 表示或強制轉換為`char`的整數值。 ```cs char ch1 = 'R';// character char ch2 = '\x0072';// hexadecimal char ch3 = '\u0059';// unicode char ch4 = (char)107;// casted from integer ``` * 字符串字面值是字符字面值的集合。 * 它們用雙引號引起來。 例如,`"Hello"`,`"Easy Programming"`等。 ```cs string firstName = "Richard"; string lastName = " Feynman"; ``` * C# also supports escape sequence characters such as: | 字符 | 含義 | | --- | --- | | `\'` | 單引號 | | `\"` | 雙引號 | | `\\` | 反斜杠 | | `\n` | 新隊 | | `\r` | 回車 | | `\t` | 水平制表符 | | `\a` | 警報 | | `\b` | 退格鍵 |
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看