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 | declarators (concept, not keyword) | declarators (keywords include user-defined types and built-in types) | ||
Declare a named constant | ||||
Create a new instance of a class | new | |||
Create a new object | CreateObject for COM objects | CoCreateInstance() (for COM objects) | new | |
Assign an object to an object variable | = | = | <- | |
Function/method does not return a value | Sub2 | |||
Overload a function or method (Visual Basic: overload a procedure or method) | (No language keyword required for this purpose) | (No language keyword required for this purpose) | ||
Refer to the current object | Me3 | |||
Make a nonvirtual call to a virtual method of the current object | MyClass::Func1(), where MyClass is a C++ class with a member function Func1. | Not applicable | Not applicable | |
Retrieve character from a string | *(p + 10) or p[10] where p is a char* or wchar_t* | str[10] where str is a string | str.Chars(10) where str is a string | |
Declare a compound data type (structure) | ||||
Initialize an object (constructor) | constructors (concept, not keyword) | Constructors, or system default type 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) | ||
Guarantee that unmanaged resources are disposed of after use | Not applicable | |||
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) | |||
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) | Not applicable | |
Declare that an object can be modified asynchronously | Not applicable | Not applicable | ||
Force explicit declaration of variables | 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 |
|
| Type inference is automatically enabled | |
Test for an object variable that does not refer to an object | pobj == NULL | obj == null | Use an option type in a match expression | |
Value of an object variable that does not refer to an object | nullptr | |||
Test for a database null expression | 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 | property: the property keyword refers to managed code |
|
Purpose |
Visual Basic |
C++ |
C# |
F# |
|---|---|---|---|---|
|
Refer to a base class |
||||
|
Declare an interface |
||||
|
Specify an interface to be implemented |
(Just derive from the interface) class C1 : public I1 |
class C1 : I1 Interfaces |
||
|
Declare a class |
||||
|
Declare a module |
static class |
static class |
||
|
Declare a partial definition of a class or structure |
Not applicable |
No direct equivalent. See Type Extensions (F#). |
||
|
Specify that a class can only be inherited. An instance of the class cannot be created |
||||
|
Specify that a class cannot be inherited |
||||
|
Declare an enumerated type |
||||
|
Declare a class constant |
const (Applied to a field declaration) |
Values are immutable (constant) by default. See Values (F#). |
||
|
Derive a class from a base class |
Class C1 : public Base (No language keyword needed for this purpose) |
class C1 : C2 |
||
|
Override a method or property |
(No language keyword required for this purpose except override for /clr compilations — see Derived Classes) |
|||
|
Declare a method that must be implemented in a deriving class |
Put = 0 at the end of the declaration (pure virtual method) |
|||
|
Declare a method that cannot be overridden |
NotOverridable (Methods are NotOverridable by default.) |
Use the Sealed attribute |
||
|
Declare a virtual method or property, or property accessor |
abstract , as described in Methods |
|||
|
Hide a base class member in a derived class |
Hiding a virtual or abstract method is not permitted |
|||
|
Declare a typesafe reference to a class method |
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 |
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 |
Not applicable |
Not applicable |
Not applicable |
|
Purpose |
Visual Basic |
C++ |
C# |
F# |
|---|---|---|---|---|
|
Exception handling |
Structured exception handling: __try, __except C++ exception handling: CLR exception handling: |
|
Purpose |
Visual Basic |
C++ |
C# |
F# |
|---|---|---|---|---|
|
Visible outside the project or assembly |
||||
|
Visible only within the assembly in which declared |
||||
|
Visible only within current or derived classes |
Not applicable |
Not applicable |
||
|
Access is limited to the current assembly or types derived from the containing class. |
protected internal |
Not applicable |
||
|
Visible only within the project (for nested classes, within the enclosing class) |
|
Purpose |
Visual Basic |
C++ |
C# |
F# |
|---|---|---|---|---|
|
Accessible outside class, project, and module |
||||
|
Accessible outside the class, but within the project or package |
public private |
|||
|
Accessible only to current and derived classes |
Not applicable |
|||
|
Only accessible within class or module |
||||
|
Specify that a function or another class has access to private members of the declaring class |
Not applicable |
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 internal |
Not applicable |
|
Purpose |
Visual Basic |
C++ |
C# |
F# |
|---|---|---|---|---|
|
Comment code |
//, (* *) for multiline comments |
|||
|
Case-sensitive? |
No |
Yes |
Yes |
Yes |
|
Call Windows API |
Not applicable |
Use Platform Invoke. |
||
|
Declare and raise an event |
Not applicable |
|||
|
Threading primitives |
Not applicable |
|||
|
Go to (branch) |
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.