資料在書中包含幾個部分,有識別字、變數、資料型別等。而當有了資料,我們可以對資料做簡單的運算處理,所以會提供一些運算子的介紹與參考資料。
識別字(Identifier)
識別字在程式中用來命名方法、變數與其他使用者定義的項目,以利於識別。而他命名規則也與C/C++類似,有以下幾點:
要以A-Z、a-z或 底線_等字元開頭,而不可為數字開頭。而後面可以接字母、數字、底線。大小寫字母在C#內,會被視為不同的字元,因此我們可以說他是一個Case sensitive(區分大小寫)的語言。
可以以中文為變數名稱,但容易造成混淆,所以不建議使用。
C#的關鍵字(Keyword)是不允許用來當作識別字的,若真的需要以關鍵字當作識別字的話,則要在關鍵字前面加上@便可使用。關鍵字定義可參考此連結,還可以根據VS的版本選擇查詢。
變數(Variable)
執行時要把程式與資料放入記憶體當中,而這個動作在高階語言當中,通常都是透過宣告(declare)變數(variable),透過電腦配置記憶體給可存放資料型別大小的空間來放變數值。宣告的目的是要賦予變數一個資料型別與變數名稱。在此要注意,C#是不允許使用未初始化過的變數,在此我們做個小測試,看會出現什麼錯誤。
按下開始編譯就會出現上圖畫面(編譯時期會出現”使用未指定的”區域變數….”),符合上述的說法。
而我們可以用兩種方法給予初值,一種是宣告時就設定,如:int myVar=20; ,或是先宣告 如:int myVar; ,之後再給指定初值 如:myVar=20,下圖有執行範例與結果 。而對於變數還有一個重要的關鍵是要用何種資料型別,這要根據你的要儲存資料的最大最小值等來判斷,而C#的資料型別可參考此連結。
字串資料型別(String Data Type)
字元資料在程式中是以單引號(‘)將字元頭尾括起來。如:’ A‘。
若一個以上的字元合併在一起,就形成了字串(String),與字元有所區的的是他必須由雙引號(“)來括起來。
字串可以做的運算有=和+,前者代表指定字串給字串變數,如:string str_1=”assign”;後者代表則是做合併兩個字串,如:string str_2=”union”+”two string”。
C#運算子(C# Operator)
上面提到字串的基本運算,因此後續就接續說明運算子。運算子(Operator)是用來指定資料做何種運算。我們可以再根據運算子需要的運算元(Operand)的數目,分類成:
一元(Unary)運算子、二元(Binary)運算子、三元(Tenary)運算子。此部分由於大致上都與C/C++相同,因此可以直接參考此連結的說明。而我們也可以對運算子的用途做幾項分類:算數運算子、關係運算子、邏輯運算子、位元運算子、移位運算子與複合指定運算子(Combination assignment operator)。這邊簡單說明複合指定運算子,就是先對運算子做一次運算後,再將結果指定給變數。
運算子的優先順序(Precedence)
而運算子的優先順序(Precedence),也不在此贅述,可參考此連結的說明。
本篇文章提及的程式碼:
(1)測試宣告變數,但沒有給予初值:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
namespace ConsoleApplication2 | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
//宣告一個整數型別的變數,名為noInitial,但未給予初值 | |
//We declare a integer variable named noIntial,but don't initialize it. | |
int noInitial; | |
//利用WriteLine這個method來顯示該變數 | |
//We use the WriteLine method to print the variable value, | |
Console.WriteLine(noInitial); | |
//等待使用者按下鍵盤 | |
//Wait for the user press the keyboard | |
Console.ReadKey(); | |
} | |
} | |
} | |
/* | |
C#主控台應用程式(ConsoleApplication)範例1_0 | |
宣告變數但並未初始化(沒給定初始值) | |
We declare the variable,but don't initialize it. | |
*/ |
(2)測試宣告變數並給予初值:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
namespace ConsoleApplication2 | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
//宣告一個整數型別的變數,名為Initial,但未給予初值 | |
//We declare a integer variable named Initial,and initialize it as 20. | |
int Initial;//int Initial=20; | |
Initial = 20; | |
//利用WriteLine這個method來顯示該變數 | |
//We use the WriteLine method to print the variable value, | |
Console.WriteLine(Initial); | |
//等待使用者按下鍵盤 | |
//Wait for the user press the keyboard | |
Console.ReadKey(); | |
} | |
} | |
} | |
/* | |
C#主控台應用程式(ConsoleApplication)範例1_1 | |
宣告變數並給予初值(20) | |
We declare the variable,and initialize it. | |
*/ |
留言
張貼留言