共用方式為


比較不同語言的關鍵字

更新:2007 年 11 月

本主題列出可用語言關鍵字做摘要的一般程式工作。如需有關需要程式碼範例之工作的詳細資訊,請參閱以不同語言的程式碼範例比較程式發展觀念

用途

Visual Basic

C++

C#

JScript

Visual FoxPro

宣告變數

Dim 陳述式 (Visual Basic)

Public (Visual Basic)

Friend (Visual Basic)

Protected (Visual Basic)

Private (Visual Basic)

Shared (Visual Basic)

Static (Visual Basic)1

宣告子 (這是觀念,而非關鍵字)

宣告子 (包括使用者定義型別與內建型別的關鍵字)

var

[隱含宣告]

PUBLIC

LOCAL

PRIVATE

宣告具名常數

Const

const

const

readonly

const

#DEFINE

建立類別的新執行個體

New

new

gcnew

new

new

NEWOBJECT( ) 函式

建立新的物件

New (Visual Basic)

CreateObject() (COM 物件專用)

CoCreateInstance() (COM 物件專用)

new

new ActiveXObject()

CREATEOBJECT( ) 函式

設定物件變數的物件

=

=

=

=

=

STORE

函式或方法不會傳回值

Sub2

void

void

void

Void (只用於 COM 伺服器)

多載一個函式或方法 (Visual Basic:多載一個程序或方法)

Overloads

(此用途不需要語言關鍵字)

(此用途不需要語言關鍵字)

(此用途不需要語言關鍵字)

(此用途不需要語言關鍵字)

參考目前物件

Me3

this

this

this

this

thisform

對目前物件的虛擬方法進行一個非虛擬呼叫

MyClass

MyClass::Func1(),其中 MyClass 是包含成員函式 Func1 的 C++ 類別。

N/A

N/A

N/A

從字串擷取字元

GetChar 函式

*(p + 10) 或 p[10],其中 p 是 char* 或 wchar_t*

str[10],其中 str 是字串

string4

str[10],其中 str 是 string

charAt

substring

substr

SUBSTR( )

宣告一個複合資料型別 (結構)

Structure

class

struct

union

__interface

struct

class

interface

class, interface

N/A

初始化一個物件 (建構函式)

Sub New()5

建構函式 (這是觀念,而非關鍵字)

建構函式或系統預設型別建構函式

類別建構函式

建構函式 (這是概念,而非關鍵字)6

Init 事件

直接終止一個物件

N/A

~ClassName

N/A

N/A

N/A

在記憶體回收並重新宣告一個 object7 之前,由系統呼叫的方法

Finalize (在 Visual Basic 6.0 中為 Class_Terminate)

Destructors (C++) (這是觀念,而非關鍵字)

解構函式

N/A

Destroy 事件

初始化所宣告的變數

Dim x As Long = 5

Dim c As New Car(FuelTypeEnum.Gas)

// 初始化一個值:

int x=5;

//配合適當的建構函式:

C c(10);

// 初始化一個值:

int x = 123;

// 或使用預設建構函式:

int x = new int();

var x = 5

var y : car = new car()

LOCAL x

x = 5

使用函式的位址

AddressOf (本運算子會以委派執行個體的形式傳回函式的參考)

delegate

delegate

使用函式的名稱而不要用括弧

N/A

回呼

將一個函式的位址傳遞至另一個回呼啟動程式的函式。如需範例,請參閱 HOW TO:在 Visual Basic 中將程序傳遞至其他程序

CALLBACK (標準型別)

callback (IDL 屬性)

delegate

N/A

N/A

宣告一個可以非同步修改的物件

N/A

volatile

volatile

N/A

N/A

強制變數的明確宣告

Option Explicit

N/A (所有變數在使用前都必須先宣告)

N/A (所有變數在使用前都必須先宣告)

快速模式 (預設值為開啟)

_VFP.LanguageOptions NEW

啟用區域型別推斷

Option Infer

測試一個不參考物件的物件變數

obj Is Nothing

pobj == NULL

obj == null

obj == undefined

obj == null

VARTYPE(obj)=="0"

一個不參考物件之物件變數的值

Nothing

nullptr

null

null

undefined

.F.

測試一個資料庫 Null 運算式

IsDbNull

N/A

N/A

x == null

ISNULL( )

測試 Variant 變數是否已初始化

N/A

N/A

N/A

x == undefined

EMPTY( )

定義預設屬性

Default

property: property 關鍵字會參照 Managed 程式碼

Indexers

N/A

N/A

物件導向程式

用途

Visual Basic

C++

C#

JScript

Visual FoxPro

參考一個基底類別

MyBase

__super

base

super

BaseClass 屬性

ParentClass 屬性

DODEFAULT()

類別::成員

宣告一個介面

Interface

__interface

interface class

interface

interface

DEFINE CLASS

指定一個要實作的介面

Implements

(從介面導出)

class C1 : public I1

class C1 : I1

介面

IMPLEMENTS

IMPLEMENTS NEW

宣告一個類別

Class

class

Classes and Structs (Managed)

class

class

DEFINE CLASSMyClassAS <ParentClass>

宣告一個模組

Module

static class

static class

N/A

N/A

宣告類別或結構的部分定義

Partial

N/A

Partial

N/A

N/A

指定一個只能被繼承的類別,無法建立類別的執行個體

MustInherit

abstract (Visual C++)8

abstract

abstract

N/A

指定一個不能被繼承的類別

NotInheritable

sealed

sealed

final

N/A

宣告一個列舉型別

Enum

enum

enum

enum

N/A

宣告一個類別常數

Const

const

const (套用到欄位宣告)

const

#DEFINE

從基底類別衍生一個類別

Class C1 Inherits C2

Class C1 : public Base (此用途不需要語言關鍵字)

class C1 : C2

class

Class c1 extends c2

DEFINE CLASSMyClass ASParentClass

覆寫方法或屬性 (Property)

Overrides

(此用途不需要語言關鍵字,除了 override 是用於 /clr 編譯之外 -- 請參閱Derived Classes)

override

(此用途不需要語言關鍵字)

(此用途不需要語言關鍵字)

宣告一個在衍生類別時必須實作的方法

MustOverride

將 = 0 放在宣告的結尾 (純虛擬方法)

abstract

abstract

(此用途不需要語言關鍵字)

宣告一個無法覆寫的方法

NotOverridable (預設的方法為 NotOverridable)。

sealed

sealed

final

N/A

宣告虛擬的方法或屬性,或是屬性存取子

Overridable

virtual

virtual

(方法的預設值是虛擬的)

N/A

在衍生類別中隱藏基底類別成員

Shadowing

new (new slot in vtable)

new (C# 參考)

new 修飾詞

N/A

宣告一個型別安全參考給類別方法

Delegate

delegate

delegate

使用函式的名稱而不要用括弧

N/A

指定一個包含您要處理其事件之物件的變數

WithEvents

N/A

(寫入程式碼 - 沒有特定關鍵字)

(寫入程式碼 - 沒有特定關鍵字)

EVENTHANDLER( ) NEW 

指定一個要呼叫其事件程序的事件

Handles (命名程序仍然可將事件程序與 WithEvents 變數關聯)

N/A

event += eventHandler;

N/A

BINDEVENTS( )

評估物件運算式一次,以便存取多個成員

With...End With

N/A

using 陳述式 (C# 參考)

with9

WITH ... ENDWITH

例外處理

用途

Visual Basic

C++

C#

JScript

Visual FoxPro

結構化例外處理

Try...Catch...Finally...End Try

Throw

__try、__except

__finally

try、catch、finally

throw

try

catch

finally

throw

TRY

   [ tryCommands ]

[ CATCH [ To VarName ] [ WHEN IExpression ] ]

   [ catchCommands ] ]

[ THROW [ eUserExpression ] ]

[ EXIT ]

[ FINALLY

   [ finallyCommands ] ]

ENDTRY

C++ 例外處理

N/A

try、catch、throw

N/A

N/A

N/A

決策結構

用途

Visual Basic

C++

C#

JScript

Visual FoxPro

決策結構 (選擇)

Select...Case...End Select

switch、case 和 default

goto

break

switch

case

default

goto

break

switch

case

break

CASE

ICASE

決策結構 (if ... then)

If...Then...Else...End If

ElseIf

if、else

if、else

if

else

IF ... ENDIF

IIF( )

迴圈結構 (條件式)

While...End While

Do...Loop

do、while

continue

do

while

continue

do、while

break、continue

DO、WHILE (子句)

迴圈結構 (反覆運算)

For...Next

For Each...Next

for

for

foreach

for (x=0;x<10;x++){...}

for (prop in obj) { print (obj[prop]);}

FOR (子句)

FOR ... ENDFOR

Continue

NEXT

FOR EACH (子句)、FOR ... ENDFOR、Continue、Next

陣列

用途

Visual Basic

C++

C#

JScript

Visual FoxPro

宣告陣列

Dim a() As Long

int x[5];

int[] x = new int[5];

var x : int[]

var arr = Array()

DIMENSION

DECLARE

初始化陣列

Dim a() As Long = {3, 4, 5}

int x[5]= {1,2,3,4,5};

int[] x = new int[5] {1, 2, 3, 4, 5};

var x : int[] = [1, 2, 3, 4, 5]

var arr = new Array(1, 2, 3, 4, 5)]

x[1] = 1

x[2] = 2

重新配置陣列

Redim

N/A

N/A

arr.length=newSize (僅供 JScript 陣列使用)10

DIMENSION

DECLARE

類別範圍

用途

Visual Basic

C++

C#

JScript

Visual FoxPro

在專案或組件之外式可見的

Public

public

public

public

N/A

只顯示在已宣告的組件內

Friend

private

internal

internal

N/A

只有在目前或衍生類別中可見

Protected

N/A

Protected

N/A

N/A

存取只限於目前的組件或衍生自包含類別的型別

Protected Friend

Type and Member Visibility

protected internal

存取範圍層次

N/A

N/A

只有在專案中可見 (對於巢狀類別而言,則是在封入類別)

Private

private

private

private

N/A

成員範圍

用途

Visual Basic

C++

C#

JScript

Visual FoxPro

在類別、專案和模組之外可存取

Public

public

public

public

(此用途不需要語言關鍵字)

在類別外以及專案或封包中可存取

Friend

public private:

internal

internal

N/A

只有目前和衍生類別可存取

Protected

protected

protected

protected

PROTECTED

只有在類別或模組中可存取

Private

private

private

private

HIDDEN

指定能存取宣告類別之 Private 成員的函式或其他類別

N/A

friend (在 C++ 中不允許)

friend

N/A

N/A

在組件和私用之內對組件進行保護

N/A

protected private

N/A

N/A

N/A

存取只限於目前的組件或衍生自包含類別的型別

Protected Friend

Type and Member Visibility

protected internal

存取範圍層次

N/A

N/A

其他存留期

用途

Visual Basic

C++

C#

JScript

Visual FoxPro

保留程序的區域變數

Static11

static

static (C# 參考)

N/A

N/A

讓類別的執行個體共用

Shared

static

static

static

N/A

其他

用途

Visual Basic

C++

C#

JScript

Visual FoxPro

註解程式碼

'

Rem

//, /* */ 多行註解

//, /* */ 多行註解

/// XML 註解

//, /* */ 多行註解

*

&&

NOTE

是否區分大小寫?

呼叫 Windows API

Declare <API>

N/A

使用 Platform Invoke

N/A

DECLARE - DLL

宣告並引發一個事件

Event

RaiseEvent

N/A

event

N/A

RAISEEVENT( ) 函式

基本執行緒

SyncLock

N/A

lock

N/A

N/A

到 (分支)

Goto

goto

goto

N/A

N/A

1 在 Visual Basic 中,唯一可以用 Static 本身來宣告變數 (例如 Static x As Long) 的地方,是在程序之內。

2 在 Visual Basic 中,以 Sub 關鍵字宣告的程序不能傳回值。如果要程序傳回值,您必須使用 Function 關鍵字來宣告。

3 在 Visual Basic 中,Me 不會在編譯時期解析,因此您可以將它當做屬性或方法的傳回值使用。

4 在 JScript 中,仍有支援 substr 函式,但不再是存取字串之字元的最佳方式。要存取在字串中特定位置的字元之最佳方式是使用括弧。例如,若要存取字串 str 的第十個字元,請使用 str[10]。

5 在 Visual Basic 中,衍生自 .NET FrameworkSystem.Object 之類別的建構函式永遠都會稱為 New。

6 在 JScript 中,建構函式不允許多載。

7 一般來說,以這種方式撰寫的程式碼,將釋放記憶體回收行程不會自動釋放的系統資源。

8 在 C++ 中,抽象類別至少包括一個純虛擬成員。

9 在 JScript 中,沒有像是在 Visual Basic 中使用的前置句號。因為可能因屬性而誤用變數,所以本功能容易造成混淆;反之亦然。亦請注意 with 陳述式會拖慢程式碼。

10 在 JScript 中,這並不會重新配置陣列,也不會增加陣列。JScript 陣列 (宣告為型別 Array) 總是多餘的和動態的。原生陣列 (宣告成 System.Array 或 type[]) 是非動態的。

11 在 Visual Basic 中,非共用類別方法的靜態區域變數會儲存在每個類別執行個體中,而非與其他語言一樣共用單一複本。當 Static 是用來宣告變數時,即使變數遺失並重新取得範圍後,仍會保留變數的值。

請參閱

參考

以不同語言的程式碼範例比較程式發展觀念

比較不同語言的運算子

比較不同語言的資料型別

比較不同語言和程式庫的控制項與程式物件

其他資源

語言對等用法

Visual J#