c# 基本語(yǔ)法
c# 是一種面向?qū)ο蟮木幊陶Z(yǔ)言。在面向?qū)ο蟮某绦蛟O(shè)計(jì)方法中,程序由各種相互交互的對(duì)象組成。相同種類的對(duì)象通常具有相同的類型,或者說(shuō),是在相同的 class 中。
例如,以 rectangle(矩形)對(duì)象為例。它具有 length 和 width 屬性。根據(jù)設(shè)計(jì),它可能需要接受這些屬性值、計(jì)算面積和顯示細(xì)節(jié)。
讓我們來(lái)看看一個(gè) rectangle(矩形)類的實(shí)現(xiàn),并借此討論 c# 的基本語(yǔ)法:
using system; namespace rectangleapplication { ? ? class rectangle ? ? { ? ? ? ? // 成員變量 ? ? ? ? double length; ? ? ? ? double width; ? ? ? ? public void acceptdetails() ? ? ? ? { ? ? ? ? ? ? length = 4.5; ? ? ? ? ? ? ? ? width = 3.5; ? ? ? ? } ? ? ? ? public double getarea() ? ? ? ? { ? ? ? ? ? ? return length * width; ? ? ? ? } ? ? ? ? public void display() ? ? ? ? { ? ? ? ? ? ? console.writeline("length: {0}", length); ? ? ? ? ? ? console.writeline("width: {0}", width); ? ? ? ? ? ? console.writeline("area: {0}", getarea()); ? ? ? ? } ? ? } ? ? ? ? class executerectangle ? ? { ? ? ? ? static void main(string[] args) ? ? ? ? { ? ? ? ? ? ? rectangle r = new rectangle(); ? ? ? ? ? ? r.acceptdetails(); ? ? ? ? ? ? r.display(); ? ? ? ? ? ? console.readline(); ? ? ? ? } ? ? } }
當(dāng)上面的代碼被編譯和執(zhí)行時(shí),它會(huì)產(chǎn)生下列結(jié)果:
length: 4.5 width: 3.5 area: 15.75
1. using 關(guān)鍵字
在任何 c# 程序中的第一條語(yǔ)句都是:
using system;
using 關(guān)鍵字用于在程序中包含命名空間。一個(gè)程序可以包含多個(gè) using 語(yǔ)句。
2. class 關(guān)鍵字
class 關(guān)鍵字用于聲明一個(gè)類。
3. c# 中的注釋
注釋是用于解釋代碼。編譯器會(huì)忽略注釋的條目。在 c# 程序中,多行注釋以 /* 開(kāi)始,并以字符 */ 終止,如下所示:
/* this program demonstrates the basic syntax of c# programming language */
單行注釋是用 '//' 符號(hào)表示。例如:
}//end class rectangle
4. 成員變量
變量是類的屬性或數(shù)據(jù)成員,用于存儲(chǔ)數(shù)據(jù)。在上面的程序中,rectangle 類有兩個(gè)成員變量,名為 length 和 width。
5. 成員函數(shù)
函數(shù)是一系列執(zhí)行指定任務(wù)的語(yǔ)句。類的成員函數(shù)是在類內(nèi)聲明的。我們舉例的類 rectangle 包含了三個(gè)成員函數(shù): acceptdetails、getarea 和 display。
6. 范例化一個(gè)類
在上面的程序中,類 executerectangle 是一個(gè)包含 main() 方法和范例化 rectangle 類的類。
7. 標(biāo)識(shí)符
標(biāo)識(shí)符是用來(lái)識(shí)別類、變量、函數(shù)或任何其它用戶定義的項(xiàng)目。在 c# 中,類的命名必須遵循如下基本規(guī)則:
- 標(biāo)識(shí)符必須以字母、下劃線或 @ 開(kāi)頭,后面可以跟一系列的字母、數(shù)字( 0 - 9 )、下劃線( _ )、@。
- 標(biāo)識(shí)符中的第一個(gè)字符不能是數(shù)字。
- 標(biāo)識(shí)符必須不包含任何嵌入的空格或符號(hào),比如 ? - +! # % ^ & * ( ) [ ] { } . ; : " ' / \。
- 標(biāo)識(shí)符不能是 c# 關(guān)鍵字。除非它們有一個(gè) @ 前綴。 例如,@if 是有效的標(biāo)識(shí)符,但 if 不是,因?yàn)?if 是關(guān)鍵字。
- 標(biāo)識(shí)符必須區(qū)分大小寫。大寫字母和小寫字母被認(rèn)為是不同的字母。
- 不能與c#的類庫(kù)名稱相同。
8. c# 關(guān)鍵字
關(guān)鍵字是 c# 編譯器預(yù)定義的保留字。這些關(guān)鍵字不能用作標(biāo)識(shí)符,但是,如果您想使用這些關(guān)鍵字作為標(biāo)識(shí)符,可以在關(guān)鍵字前面加上 @ 字符作為前綴。
在 c# 中,有些關(guān)鍵字在代碼的上下文中有特殊的意義,如 get 和 set,這些被稱為上下文關(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 |