Поделиться через


Keywords Compared in Various Languages

This topic lists common programming tasks that can be summarized with a language keyword. For more information about tasks that need code examples, see Programming Concepts Compared in Various Languages, with Code Examples.

Purpose

Visual Basic

C++

C#

F#

Declare a variable

Dim

Public

Friend

Protected

Private

Shared

Static1

declarators (concept, not keyword)

declarators (keywords include user-defined types and built-in types)

let Bindings

Values

Declare a named constant

Const

const

const

readonly

let Bindings

Values

Create a new instance of a class

New

new

gcnew

new

new

Create a new object

New

CreateObject for COM objects

CoCreateInstance() (for COM objects)

new 

new

Assign an object to an object variable

=

=

=

<-

Function/method does not return a value

Sub2

void

void

Unit Type

Overload a function or method (Visual Basic: overload a procedure or method)

Overloads

(No language keyword required for this purpose)

(No language keyword required for this purpose)

override

Refer to the current object

Me3

this

this

Use the self-identifier as described in Methods or Members

Make a nonvirtual call to a virtual method of the current object

MyClass

MyClass::Func1(), where MyClass is a C++ class with a member function Func1.

Not applicable

Not applicable

Retrieve character from a string

GetChar

*(p + 10) or p[10] where p is a char* or wchar_t*

str[10] where str is a string

string

str.Chars(10) where str is a string

Strings

Declare a compound data type (structure)

Structure

Class

Interface

class

struct

union

__interface

struct

class

interface

Classes

Initialize an object (constructor)

Sub New()4

constructors (concept, not keyword)

Constructors, or system default type constructors

Class Constructors

Constructors

Terminate an object directly

Not applicable

~ClassName

Not applicable

Not applicable

Method called by the system just before garbage collection reclaims an object5

Finalize (in Visual Basic 6.0, Class_Terminate)

Destructors (C++) (concept, not keyword)

Destructors

Finalize

Guarantee that unmanaged resources are disposed of after use

Using

Not applicable

using

Resource Management

Initialize a variable where it is declared

Dim x As Long = 5

Dim c As New Car(FuelTypeEnum.Gas)

// initialize to a value:

int x=5;

//with an appropriate constructor:

C c(10);

// initialize to a value:

int x = 123;

// or use default constructor:

int x = new int();

let x = 123

Take the address of a function

AddressOf (This operator returns a reference to a function in the form of a delegate instance)

delegate

delegate

Functions

Callback

Pass the address of one function to another that calls the invoker back. For an example, see How to: Pass Procedures to Another Procedure in Visual Basic.

CALLBACK (a standard type)

callback (IDL attribute)

delegate

Not applicable

Declare that an object can be modified asynchronously

Not applicable

volatile

volatile

Not applicable

Force explicit declaration of variables

Option Explicit

Not applicable (All variables must be declared prior to use)

Not applicable (All variables must be declared prior to use)

Not applicable (All variables must be declared prior to use)

Enable local type inference

Option Infer

Type inference is automatically enabled

Test for an object variable that does not refer to an object

obj Is Nothing

pobj == NULL

obj == null

Use an option type in a match expression

Option

Match Expressions

Value of an object variable that does not refer to an object

Nothing

nullptr

null

Null Values

Test for a database null expression

IsDBNull

Not applicable

Not applicable

Not applicable

Test whether a Variant variable has been initialized

Not applicable

Not applicable

Not applicable

Not applicable

Define a default property

Default

property: the property keyword refers to managed code

Indexers

Indexed Properties

Object-Oriented Programming

Purpose

Visual Basic

C++

C#

F#

Refer to a base class

MyBase

__super

base

base

Declare an interface

Interface

__interface

interface class

interface

interface

Specify an interface to be implemented

Implements

(Just derive from the interface)

class C1 : public I1

class C1 : I1

Interfaces

interface

Declare a class

Class

class

Classes and Structs (Managed)

class

type

Declare a module

Module

static class

static class

module

Declare a partial definition of a class or structure

Partial

Not applicable

Partial

No direct equivalent. See Type Extensions (F#).

Specify that a class can only be inherited. An instance of the class cannot be created

MustInherit

abstract (Visual C++)6

abstract

Abstract Classes

Specify that a class cannot be inherited

NotInheritable

sealed

sealed

SealedAttribute

Declare an enumerated type

Enum

enum

enum

type

Declare a class constant

Const

const

const (Applied to a field declaration)

Values are immutable (constant) by default. See Values (F#).

Derive a class from a base class

Class C1 Inherits C2

Class C1 : public Base (No language keyword needed for this purpose)

class C1 : C2

class

inherit

Override a method or property

Overrides

(No language keyword required for this purpose except override for /clr compilations — see Derived Classes)

override

override

Declare a method that must be implemented in a deriving class

MustOverride

Put = 0 at the end of the declaration (pure virtual method)

abstract

abstract

Declare a method that cannot be overridden

NotOverridable (Methods are NotOverridable by default.)

sealed

sealed

Use the Sealed attribute

Declare a virtual method or property, or property accessor

Overridable

virtual

virtual

abstract, as described in Methods

Hide a base class member in a derived class

Shadowing

new (new slot in vtable)

new

Hiding a virtual or abstract method is not permitted

Declare a typesafe reference to a class method

Delegate

delegate

delegate

myObj.myFunction where myObj is an object and myMethod is a method available on that object

Specify that a variable can contain an object whose events you wish to handle

WithEvents

Not applicable

(Write code - no specific keyword)

Not applicable

Specify the events for which an event procedure will be called

Handles (Event procedures can still be associated with a WithEvents variable by naming pattern)

Not applicable

event += eventHandler;

Not applicable

Evaluate an object expression once, in order to access multiple members

With... End With

Not applicable

Not applicable

Not applicable

Exception Handling

Purpose

Visual Basic

C++

C#

F#

Exception handling

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

Throw

Structured exception handling:__try, __except

__finally

C++ exception handling:

try, catch, throw

CLR exception handling:

Exception Handling under /clr

try, catch, finally

throw

try

with

finally

Decision Structures

Purpose

Visual Basic

C++

C#

F#

Decision structure (selection)

Select... Case... End Select

switch, case, default

goto

break

switch, case

default

goto

break

Match Expressions

Decision structure (if ... then)

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

ElseIf

if, else

if, else

if...then...else

Loop structure (conditional)

While... End While

Do...Loop

do, while

continue

do

while

continue

while...do

Loop structure (iteration)

For... Next

For Each... Next

for

for

foreach

for...to

for...in

Arrays

Purpose

Visual Basic

C++

C#

F#

Declare an array

Dim a() As Long

int x[5];

int[] x = new int[5];

let x = [| 1; 2; 3; 4; 5 |]

Arrays

Initialize an array

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};

let x = [| 1; 2; 3; 4; 5 |]

Arrays

Reallocate array

Redim

Not applicable

Not applicable

Not applicable

Class Scope

Purpose

Visual Basic

C++

C#

F#

Visible outside the project or assembly

Public

public

public

public

Visible only within the assembly in which declared

Friend

private

internal

internal

Visible only within current or derived classes

Protected

Not applicable

Protected

Not applicable

Access is limited to the current assembly or types derived from the containing class.

Protected Friend

Type and Member Visibility

protected internal

Accessibility Levels

Not applicable

Visible only within the project (for nested classes, within the enclosing class)

Private

private

private

private

Member Scope

Purpose

Visual Basic

C++

C#

F#

Accessible outside class, project, and module

Public

public

public

public

Accessible outside the class, but within the project or package

Friend

public private

internal

internal

Accessible only to current and derived classes

Protected

protected

protected

Not applicable

Only accessible within class or module

Private

private

private

private

Specify that a function or another class has access to private members of the declaring class

Not applicable

friend

Not applicable

Not applicable

Protected inside the assembly and private to other assemblies

Not applicable

protected private

Not applicable

Not applicable

Access is limited to the current assembly or types derived from the containing class

Protected Friend

Type and Member Visibility

protected internal

Accessibility Levels

Not applicable

Miscellaneous Lifetime

Purpose

Visual Basic

C++

C#

F#

Preserve procedure's local variables

Static7

static

static

Not applicable

Shared by all instances of a class

Shared

static

static

static

Miscellaneous

Purpose

Visual Basic

C++

C#

F#

Comment code

'

Rem

//, /* */ for multiline comments

//, /* */ for multiline comments

/// for XML comments

//, (* *) for multiline comments

Case-sensitive?

No

Yes

Yes

Yes

Call Windows API

Declare <API>

Not applicable

use Platform Invoke

Use Platform Invoke.

See External Functions (F#).

Declare and raise an event

Event

RaiseEvent

Not applicable

event

Events

Threading primitives

SyncLock

Not applicable

lock

lock

Go to (branch)

Goto

goto

goto

Not applicable

1 In Visual Basic, the only place where Static can be used by itself to declare a variable — for example, Static x As Long — is within a procedure.

2 In Visual Basic, procedures declared with the Sub keyword cannot return values. If a procedure is to return a value, you must declare it with the Function keyword.

3 In Visual Basic, Me is not resolved at compile time, so you can use it as the return value of a property or method.

4 In Visual Basic, constructors for classes derived from .NET Framework System.Object are always named New.

5 Typically, code in such a method frees system resources that would not automatically be freed by the garbage collector.

6 In C++, an abstract class includes at least one pure virtual member.

7 In Visual Basic, static local variables of nonshared class methods are stored per class instance rather than sharing a single copy, as in other languages. When Static is used to declare a variable, the value of that variable is preserved even if the variable loses and then regains scope.

See Also

Reference

Programming Concepts Compared in Various Languages, with Code Examples

Operators Compared in Various Languages

Data Types Compared in Various Languages

Controls and Programmable Objects Compared in Various Languages and Libraries

Other Resources

Language Equivalents