using 關(guān)鍵字
在任何 C# 程序中的第一條語(yǔ)句都是:
using System;
using 關(guān)鍵字用于在程序中包含命名空間。一個(gè)程序可以包含多個(gè) using 語(yǔ)句。
class 關(guān)鍵字
class 關(guān)鍵字用于聲明一個(gè)類(lèi)。
C# 中的注釋
注釋是用于解釋代碼。編譯器會(huì)忽略注釋的條目。在 C# 程序中,多行注釋以 /* 開(kāi)始,并以字符 */ 終止,如下所示:
/* This program demonstrates The basic syntax of C# programming Language */
單行注釋是用 '//' 符號(hào)表示。例如:
}//end class Rectangle
成員變量
變量是類(lèi)的屬性或數(shù)據(jù)成員,用于存儲(chǔ)數(shù)據(jù)。在上面的程序中,Rectangle 類(lèi)有兩個(gè)成員變量,名為 length 和 width。
成員函數(shù)
函數(shù)是一系列執(zhí)行指定任務(wù)的語(yǔ)句。類(lèi)的成員函數(shù)是在類(lèi)內(nèi)聲明的。我們舉例的類(lèi) Rectangle 包含了三個(gè)成員函數(shù): AcceptDetails、GetArea 和 Display。
實(shí)例化一個(gè)類(lèi)
在上面的程序中,類(lèi) ExecuteRectangle 是一個(gè)包含 Main() 方法和實(shí)例化 Rectangle 類(lèi)的類(lèi)。
標(biāo)識(shí)符
標(biāo)識(shí)符是用來(lái)識(shí)別類(lèi)、變量、函數(shù)或任何其它用戶(hù)定義的項(xiàng)目。在 C# 中,類(lèi)的命名必須遵循如下基本規(guī)則:
標(biāo)識(shí)符必須以字母開(kāi)頭,后面可以跟一系列的字母、數(shù)字( 0 - 9 )或下劃線(xiàn)( _ )。標(biāo)識(shí)符中的第一個(gè)字符不能是數(shù)字。
標(biāo)識(shí)符必須不包含任何嵌入的空格或符號(hào),比如 ? - +! @ # % ^ & * ( ) [ ] { } . ; : " ' / \。但是,可以使用下劃線(xiàn)( _ )。
標(biāo)識(shí)符不能是 C# 關(guān)鍵字。
C# 關(guān)鍵字
關(guān)鍵字是 C# 編譯器預(yù)定義的保留字。這些關(guān)鍵字不能用作標(biāo)識(shí)符,但是,如果您想使用這些關(guān)鍵字作為標(biāo)識(shí)符,可以在關(guān)鍵字前面加上 @ 字符作為前綴。
在 C# 中,有些標(biāo)識(shí)符在代碼的上下文中有特殊的意義,如 get 和 set,這些被稱(chēng)為上下文關(guān)鍵字(contextual keywords)。
下表列出了 C# 中的保留關(guān)鍵字(Reserved Keywords)和上下文關(guān)鍵字(Contextual Keywords):
保留關(guān)鍵字 | ||||||
abstract | as | base | bool | break | byte | case |
catch | char | checked | class | const | continue | decimal |
default | delegate | do | double | else | enum | event |
explicit | extern | false | finally | fixed | float | for |
foreach | goto | if | implicit | in | in (generic modifier) | int |
interface | internal | is | lock | long | namespace | new |
null | object | operator | out | out (generic modifier) | override | params |
private | protected | public | readonly | ref | return | sbyte |
sealed | short | sizeof | stackalloc | static | string | struct |
switch | this | throw | true | try | typeof | uint |
ulong | unchecked | unsafe | ushort | using | virtual | void |
volatile | while | |||||
上下文關(guān)鍵字 | ||||||
add | alias | ascending | descending | dynamic | from | get |
global | group | into | join | let | orderby | partial (type) |
partial (method) | remove | select | set |