中文字幕一区二区人妻电影,亚洲av无码一区二区乱子伦as ,亚洲精品无码永久在线观看,亚洲成aⅴ人片久青草影院按摩,亚洲黑人巨大videos

C# 基本語法

C# 是一種面向?qū)ο蟮木幊陶Z言。在面向?qū)ο蟮某绦蛟O(shè)計方法中,程序由各種相互交互的對象組成。相同種類的對象通常具有相同的類型,或者說,是在相同的 class 中。

例如,以 Rectangle(矩形)對象為例。它具有 length 和 width 屬性。根據(jù)設(shè)計,它可能需要接受這些屬性值、計算面積和顯示細節(jié)。

讓我們來看看一個 Rectangle(矩形)類的實現(xiàn),并借此討論 C# 的基本語法:

實例

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í)行時,它會產(chǎn)生下列結(jié)果:

Length: 4.5
Width: 3.5
Area: 15.75

using 關(guān)鍵字

在任何 C# 程序中的第一條語句都是:

using System;

using 關(guān)鍵字用于在程序中包含命名空間。一個程序可以包含多個 using 語句。

class 關(guān)鍵字

class 關(guān)鍵字用于聲明一個類。

C# 中的注釋

注釋是用于解釋代碼。編譯器會忽略注釋的條目。在 C# 程序中,多行注釋以 /* 開始,并以字符 */ 終止,如下所示:

/* This program demonstrates
The basic syntax of C# programming 
Language */

單行注釋是用 '//' 符號表示。例如:

}//end class Rectangle    

成員變量

變量是類的屬性或數(shù)據(jù)成員,用于存儲數(shù)據(jù)。在上面的程序中,Rectangle 類有兩個成員變量,名為 lengthwidth。

成員函數(shù)

函數(shù)是一系列執(zhí)行指定任務(wù)的語句。類的成員函數(shù)是在類內(nèi)聲明的。我們舉例的類 Rectangle 包含了三個成員函數(shù): AcceptDetails、GetAreaDisplay

實例化一個類

在上面的程序中,類 ExecuteRectangle 是一個包含 Main() 方法和實例化 Rectangle 類的類。

標(biāo)識符

標(biāo)識符是用來識別類、變量、函數(shù)或任何其它用戶定義的項目。在 C# 中,類的命名必須遵循如下基本規(guī)則:

  • 標(biāo)識符必須以字母、下劃線或 @ 開頭,后面可以跟一系列的字母、數(shù)字( 0 - 9 )、下劃線( _ )、@。
  • 標(biāo)識符中的第一個字符不能是數(shù)字。
  • 標(biāo)識符必須不包含任何嵌入的空格或符號,比如 ? - +! # % ^ & * ( ) [ ] { } . ; : " ' / 。
  • 標(biāo)識符不能是 C# 關(guān)鍵字。除非它們有一個 @ 前綴。 例如,@if 是有效的標(biāo)識符,但 if 不是,因為 if 是關(guān)鍵字。
  • 標(biāo)識符必須區(qū)分大小寫。大寫字母和小寫字母被認(rèn)為是不同的字母。
  • 不能與C#的類庫名稱相同。

C# 關(guān)鍵字

關(guān)鍵字是 C# 編譯器預(yù)定義的保留字。這些關(guān)鍵字不能用作標(biāo)識符,但是,如果您想使用這些關(guān)鍵字作為標(biāo)識符,可以在關(guān)鍵字前面加上 @ 字符作為前綴。

在 C# 中,有些關(guān)鍵字在代碼的上下文中有特殊的意義,如 get 和 set,這些被稱為上下文關(guān)鍵字(contextual keywords)。

下表列出了 C# 中的保留關(guān)鍵字(Reserved Keywords)和上下文關(guān)鍵字(Contextual Keywords):

保留關(guān)鍵字
abstractasbaseboolbreakbytecase
catchcharcheckedclassconstcontinuedecimal
defaultdelegatedodoubleelseenumevent
explicitexternfalsefinallyfixedfloatfor
foreachgotoifimplicitinin (generic
modifier)
int
interfaceinternalislocklongnamespacenew
nullobjectoperatoroutout
(generic
modifier)
overrideparams
privateprotectedpublicreadonlyrefreturnsbyte
sealedshortsizeofstackallocstaticstringstruct
switchthisthrowtruetrytypeofuint
ulonguncheckedunsafeushortusingvirtualvoid
volatilewhile
上下文關(guān)鍵字
addaliasascendingdescendingdynamicfromget
globalgroupintojoinletorderbypartial
(type)
partial
(method)
removeselectset