1.標(biāo)識(shí)符
using System;
namespace WelcomeYou
{
//"歡迎你“的控制臺(tái)程序
class Program
{
static void Main(string[] args)
{
//定義輸出文本變量
string welcomeText = "歡迎你";
//輸出文本輸出到控制臺(tái)
Console.WriteLine(welcomeText);
//讓控制臺(tái)程序可見(jiàn)用戶(hù)輸入,并停留
Console.ReadKey();
}
}
}
像WelcomeYou,Program,Main,welcomeText就標(biāo)識(shí)符,(值得注意的事,習(xí)慣上c#變量名采用駝峰法命名,其他的采用帕斯卡法命名)。
ps:
駝峰法:首個(gè)單詞首字母小寫(xiě),后面的單詞首字母大寫(xiě)。
帕斯卡法:首個(gè)單詞首字母大寫(xiě),后面的單詞首字母大寫(xiě)。
標(biāo)識(shí)符不允許數(shù)字開(kāi)頭,開(kāi)頭必須是_或者字符開(kāi)頭(包括漢語(yǔ)字符)。標(biāo)識(shí)符不能和關(guān)鍵字一樣,如果要使用關(guān)鍵字作為標(biāo)識(shí)符,前面要加@,比如@class.
按照習(xí)慣來(lái)說(shuō),標(biāo)識(shí)符的命名要有意義,(對(duì)類(lèi)的成員命名的時(shí)候一般都是名詞,方法一般都是動(dòng)詞)。
2.關(guān)鍵字
在C#里面關(guān)鍵字事不能作為標(biāo)識(shí)符存在。在前面的代碼中namespace,class ,static ,string 都是為關(guān)鍵字。
官方文檔
C#中有77個(gè)常規(guī)關(guān)鍵字
| abstract | as | base | bool | break |
|---|---|---|---|---|
| byte | case | catch | char | checked |
| class | const | continue | decimal | default |
| dekegate | do | double | else | enum |
| event | explicit | extern | false | finally |
| fixed | float | for | foreach | goto |
| if | implicit | in | int | interface |
| internal | is | lock | long | namespace |
| new | null | object | operator | out |
| override | params | private | protected | public |
| readonly | ref | return | sbyte | sealed |
| short | sizeof | stackalloc | static | string |
| struct | switch | this | throw | true |
| try | typeof | unit | ulong | unchecked |
| unsafe | ushort | using | virtual | void |
| volatile | while |
還有25個(gè)上下文關(guān)鍵詞
| add | alias | ascending | async | await |
|---|---|---|---|---|
| descending | dynamic | from | get | global |
| group | into | join | let | orderby |
| partial (type) | partial (method) | remove | select | set |
| value | var | where | where | yield |