C# MASTER

Keywords

Back to Table of Contents

compare to JavaTM keywords

Here is a list of the keywords of the C# programming language, which cannot be used as variable names:

Of the set below, the following are all the built-in types of the C# language:

C# Type: bool byte sbyte char short ushort int uint long ulong float double decimal
CTS Type: Boolean Byte SByte Char Int16 UInt16 Int32 UInt32 Int64 UInt64 Single Double Decimal
Size in Bytes: 1 1 1 2 2 2 4 4 8 8 4 8 16

In C#, the C# types and the CTS types are interchangeable in your code. For example, bool and Boolean are identical wherever they appear in your code, and there is no difference between using one or the other. The compiler will produce the exact same Intermediate Language executables and DLLs.

Note that CTS stands for Common Type System, which defines the types that all Common Language Infrastructure (CLI) languages can agree on, though not necessarily support. Note that all C# types are lowercase, while the CTS types have capital letters. C# is case sensitive. However, ignoring the CTS types, you can be assured that all the keywords in C# are lowercase.

abstract Used in declartion of classes and methods in classes declared abstract. An astract method has no implementation, as in an interface, and any class that has an abstract method must be declared abstract. It is intended to be extended by child classes that implement the abstract methods.
as Binary operator that casts the object on the left to the type on the right, returning null if it fails. Exceptions are never thrown by this operator. see is.
base Equivalent to JavaTM's super keyword. It accesses members of the base class.
bool, Boolean The logical datatype type that holds either a true or false value. Note, this value can not be converted to a number, as is the case with other languages.
break A control flow command that breaks out of for, do while, while loops and switch sequences. Unlike in JavaTMit cannot be used with labels. To break out of outer loops in C#, you can use goto.
byte, Byte The one-byte value datatype for an 8-bit unsigned integer.
case A control flow method used in switch commands to test a value for executing a block of code. See switch.
catch An Exception handling command to catch Exceptions in a try catch block. See try.
char, Char The two-byte value datatype representing a single Unicode character.
checked A statement or operator that will check for arithmetic bounds overflow within a specified block of code or an expression and throw an OverflowException if such occurs. Opposite of unchecked.
class Used to declare a module of code that is a class. An Object is an instance of a class. A class may extend from one base class and may implement multiple interfaces, and unless it is declared sealed, it may be extended by other classes. object, which is an alias of System.Object, is the default base class and the ultimate root of the entire class hierarchy and hence has no super class.
const A modifier used to make a variable of field constant. It is only evaluated at compile time so it must be a predefined type.
continue A control flow command that is used to immediately jump to the next iteration of a for, while, or do while loop. Example:
for(int i = 0; i <= 5; ++i){
		if (i < 4) continue;
		Console.WriteLine(""+i);
}
//this prints:
4
5
decimal, Decimal A sixteen-byte decimal value datatype, used to store a limited number of real numbers precisely. A key use of this datatype is for money. Note that decimal is not a floating point number, because it defines numbers with precision, rather than as approximations.
default The control flow command that is used to signify the default of a switch command. See switch.
delegate A type for defining a method signature, used for making type-safe objects that point to a method or list of methods which can be invoked at a later time.
do A control flow command used to construct a loop that executes at least once, and until the condition in the while expression at the end of the block evaluates to false.
double, Double The eight-byte value datatype for a 64-bit floating point real number.
else A control flow command that defines a block to be executed as an alternative to an if block or a previous else block.
enum A distinct value type used to hold a set of named constants.
//Example:
enum MyShapeVertices { Circle = 0, Square = 4, Triangle = 3 };

Console.WriteLine("A {0} has {1} corners.", MyShapeVertices.Square, (int)MyShapeVertices.Square);
This prints out:
A Square has 4 corners.
event A member modifier for a delegate field or property which limits access to the delegate to the two methods, which are automatically generated: add_XXX and remove_XXX where XXX is the delegate field or property name, along with the two corresponding operators: += and -=, which essentially saves the programmer a lot of work.
explicit A modifier for an operator defining a custom type conversion that will require the programmer to explicitly cast one object or value to another.
extern A method modifier indicating that it is implemented in unmanaged code, that is, external to C# code.
false One of the two possible values for the bool or Boolean value datatype.
finally An exception handling command that describes a block of code that is guaranteed to always executed at the end of a try/catch sequence. Used to guarantee resources are closed that are opened within a try/catch sequence by putting the resource closing code within the finally block. See try. Also, see the keyword using.
fixed A statement used to lock a managed reference variable in memory so that unsafe code (such as pointers) can safely access it. The C# compiler requires the usage of fixed in such cases. Otherwise, the garbage collector could move the variable in memory.
float, Single The four-byte value datatype for a 32-bit floating point real number.
for A control flow command that defines a loop as such:
for(initializing; boolean condition; increments){...}

example:
for (int i=0; i < 10; i++){ 
   Console.WriteLine("count = {0}.",i); 
}
foreach A control flow command that defines a loop for iterating through all the items of an IEnumerable, such as an array. Example:
int[] A = new int[]{ 1, 2, 3, 4};
for (int i in A){ 
   Console.WriteLine("count = {0}.",i); 
}
get The name of the accessor of a property that returns (gets) its value.
goto A jump statement for jumping to another section of code marked by a label. Note that the goto label must be within the same method. Example:
bool SkipHello = true;
if (SkipHello) goto AfterHello;
Console.WriteLine("Hello World!");
AfterHello:
Console.WriteLine("I am a C# program.");
However, be careful not to make infinite loops or spaghetti code!
if A control flow statement that evaluates a boolean condition to determine whether to execute a block of code.
implicit A modifier for an operator defining a custom type conversion that will not require the programmer to explicitly cast one object or value to another
in An operator for iterating through the items in an IEnumerable, such as an array, in a foreach statement. In the foreach statement, the IEnumerable is placed on the right and the variable to hold each item is placed on the left of the operator in, and this variable is usable in the loop block that follows the foreach statement. see foreach.
int, Int32 The four-byte value datatype for a 32-bit signed integer.
interface The declaration of a module of code that is an alternative to class or struct, but which contains only abstract members which are meant to be implemented by a class or struct to enable instances of it through instantiating its implementing classes or structs.
internal An access modifier used to apply to types to limit their scope to within their containing assembly.
is Binary operator that returns a boolean true if the object on the left can be cast to the type on the right, returning false if not. Equivalent to JavaTM's instanceof operator. see as.
lock Used to mark a block of code as a critical section that can be accessed by only one thread at a time for the object locked. Equivalent to JavaTM's synchronized keyword when used for blocks of code.
long, Int64 The eight-byte value datatype for a 64-bit signed integer.
namespace Used to declare a scope, enabling globally unique names for types. Equivalent to JavaTM's package keyword, but also allows nesting of namespaces within the same code file.
new Used to instantiate new objects, allocating memory for them on the heap if they are references types, or initializing them if they are value types. Also, this is required to designate a method declaration identical to a non-virtual method in a base class. This is not called overriding but hiding the method. see virtual.
null The value an Object that indicates it refers to nothing.
object The root object from which all other types are derived from, and an alias to System.Object.
operator A method modifier to indicate an operator is being overloaded by the defining type.
out A method parameter modifier used to indicate that it is passed by reference, but must be set within the method itself. This enables the effect of a method returning more than one value or object.
override A class method modifier for a method in a subclass to override a virtual method of the same signature in a base class. see virtual.
params The keyword that simply enables a variable number of arguments in a method call. It must be used only to designate the last parameter, which must be an array. To call such a function, any number of arguments may be supplied for the final parameter, providing they are all of the same type as the underlying array is defined.
private An access modifier used to apply to types to limit their scope to within their containing type.
protected An access modifier used to apply to types to limit their scope to within their containing type and to derived types.
public An access modifier used to apply to types to make them accessable to all other types.
readonly A field modifier allowing a field to be assigned only once, either in its declaration or in the containing type's constructor.
ref A method parameter modifier used to indicate that it is passed by reference, and is assigned before being passed to the method. This enables a value object to be modified from within a method.
return A flow control statement to exit from a method. If the method has a return value (is not void) the return command must be followed by a value of the return type specified in the method declaration.
sbyte, SByte The one-byte value datatype for an 8-bit signed integer.
sealed Used in declarations of classes to prevent them from being extended. Also used in declarations of virtual methods to prevent overriding in subclasses.
set The name of the accessor of a property that assigns (sets) its value.
short, Int16 The two-byte value datatype for a 16-bit signed integer.
sizeof An operator that returns the size in bytes of the datatype to its right.
stackalloc An operator used in unsafe code to allocate a block of memory on the stack. Equivalent to _alloca in C.
static Used to declare members of types as applying to the type and not of the generated instances, although the instances have access to them. Static methods cannnot access instance members and can be called without making an instance of the class in which they are declared. A static class can have only static members.
string A reference type to hold an immutable set of Unicode Characters, and an alias of System.String.
struct A value type which can have data and members like a class but which cannot be extended.
switch A flow control statment that is used with case and break statements to select a single block of code for execution: Example:
switch (expression){
case 1: DoSomeCode(); break;
case 2: DoSomethingElse(); break;
default: DoThis(); break;
}
this Refers to the current instance of the class or struct containing the executing code.
throw Used to throw an Exception out of the block of code possibly to be caught by a catch statement following the try block in which the exception occurred.
true One of the two possible values for the bool or Boolean value datatype.
try Used in Exception handling to execute code that may throw exceptions. Example:
try{
    DoMyCode();
}
catch(Exception ex){ 
    handleMyEx(); 
}
finally{
    closeMyResources();
}
typeof An operator that returns a System.Type object indicating an object's type.
uint, UInt32 The four-byte value datatype for a 32-bit unsigned integer.
ulong, UInt64 The eight-byte value datatype for a 64-bit unsigned integer.
unchecked A statement or operator that will prevent checking for arithmetic bounds overflow within a specified block of code or an expression and not throw an OverflowException if such occurs. Opposite of checked and used only when the /checked compiler flag is enabled. By default, C# is all unchecked.
unsafe A modifier for a method or block used to enable usage of pointers, bypassing the Common Language Runtime's (CLR's) memory management.
ushort, Int16 The two-byte value datatype for a 16-bit unsigned integer.
using three uses:
  1. As a directive, to specify a namespace so that types defined in those namespaces can be referred to without specifying their fully qualified name within the code file where the using directive appears.
    using System; //contains Console object.
    
    Console.WriteLine("Howdee!");
    
  2. As a directive, to create an alias for a namespace or type within the code file where the using directive appears.
    using MyConsole = System.Console;
    
    MyConsole.WriteLine("Howdee!");
    
  3. As a replacement to the try finally block where an object that implements the IDisposable interface is specified as:
    using ( MyDisposableObj A = getTheObj() ){ 
       A.UseAMethod(); 
       A.AnotherMethod();
    }
    which ensures that the object's Dispose() method will be called at the end of the block without any further code.
value The name of the variable containing the value to be set within the scope of a property's set accessor.
var Known as the "contextual var token". Although technically not a keyword, the compiler treats var as a keyword when used in the context of replacing the formal datatype declaration with an implicit type within a method or property scope. Note that implicitly typed data is still strongly typed. Also, var cannot be used to define return values, parameters, or field data of a type.
virtual A class method modifier which enables a subclass to override the method. Without using virtual, a method by the same name in the subclass cannot override, but requires using the new keyword to hide the method, so that which method is called will not be consistent, but depends on the current variable type. By using virtual ensures consistency in method calls when overridden in the subclasses regardless of the current variable type of a polymorphic object. Use virtual when implementing methods of interfaces in your classes to ensure that those methods can also be properly overridden.
void Used in method declarations to signify that the method does not return a value.
volatile Used to declare a variable as possibly changing unexpectedly from an external source.
while A flow control that repeats a loop for as long as its boolean condition is true. Example:
while (a > b){
  b++;
  doSomething();
}

*OracleTM and JavaTM are registered trademarks of Oracle and or its affiliates. Other names may be trademarks of their respective owners.*