Appendix A. C# Keywords

This appendix lists the various keywords in C# that are predefined and have special meanings for the compiler. It is important to familiarize yourself with these keywords because they cannot be used as identifiers in your program. For example, this is a keyword in C# that is used to refer to the current instance of a class. Hence, this cannot be used as an identifier:

string this = "Hello, World"; //---error---

To use a keyword as an identifier, you need to prefix the keyword with the @ character. The following statement is valid:

string @this = "Hello, World"; //---ok---

In C# 3.0, Microsoft introduces the concept of context keywords. Contextual keywords have special meaning in a particular context and can be used as an identifier outside the context. For example, the set and get contextual keywords are used as accessors in a property definition, together with the value keyword, like this:

public class Point
    {
        Single _x;
        public Single x {
            get {
                return _x;
            }
            set {
                _x = value;
            }
        }
    }

In this example, the get, set, and value contextual keywords have special meanings in a property definition (x). Using these contextual keywords within the property definition as identifiers is not valid. However, outside the property definition, you can use them as identifiers:

static void Main(string[] args)
    {
           string get = "some string here...";
           int set = 5;
           double value = 5.6;
    }

The beauty of contextual keywords is that as the C# language evolves, new keywords can be added to the language without breaking programs written using the earlier version of the language.

C# Reserved Keywords

The following table describes the reserved keywords used in C#.

Keyword

Description

abstract

A modifier that can be used with classes, methods, properties, indexers, and events. Use it to indicate that a class is intended to be used as a base class of other classes, and abstract methods must be implemented by classes that derive from the abstract class.

as

An operator that performs conversion between compatible reference types.

base

Used to access members of a base class from a derived class.

bool

A C# alias of the System.Boolean .NET Framework type. Its value can either true, false, or null.

break

Used to transfer control out of a loop or switch statement.

byte

Specifies a data type that can stores unsigned 8-bit integer values from 0 to 255.

case

Used together with the switch statement. It specifies the value to be matched so that control can be transferred to the case statement.

catch

Used with a try block to handle one or more exceptions.

char

Specifies a data type that can store a 16-bit Unicode character from U+0000 to U+ffff.

checked

Used to explicitly enable overflow-checking integer operations.

class

Used to declare classes.

const

Used to specify a field or variable whose value cannot be modified.

continue

Used within a loop such that control is transferred to the next iteration.

decimal

Specifies a data type representing a 128-bit data. It can approximately represent a number from ±1.0 × 10–28 to ±7.9 × 1028.

default

Used within a switch statement to indicate the default match if none of the other case statements is matched. Can also be used in generics to specify the default value of the type parameter.

delegate

Used to declare a reference type variable that references a method name/anonymous method.

do

Executes a block of code repeatedly until a specified expression returns false. Used together with the while keyword to form a do-while statement.

double

Specifies a data type that represents a 64-bit floating point number. It can approximately represent a number from ±5.0 × 10–324 to ±1.7 × 10308.

else

Used with the if keyword to form an if-else statement. else defines the block that will be executed if the expression specified in the if statement is evaluated to false.

enum

Used to define an enumeration.

event

Used to define an event within a class.

explicit

Defines a cast operation that requires the programmer to explicitly select the cast to be performed.

extern

Declares a method that is implemented externally.

false

Used as either an operator or as a literal. One of the possible values in a bool variable.

finally

Used in a try-catch block to contain code that cleans up the code even if an exception occurs. Statements contained within a finally block are always executed.

fixed

Prevents the garbage collector from relocating a movable variable.

float

Specifies a data type that represents a 32-bit floating point number. It can approximately represent a number from ±1.5 × 10–45 to ±3.4 × 1038.

for

Encloses a block of statements that will be executed repeatedly until a specified expression returns false.

foreach

Used to iterate through a collection of items.

goto

Used to transfer control of a program to a labeled statement.

if

Determines if a statement (or block of statements) is to be executed based on the result of a Boolean expression.

implicit

Used to declare an implicit cast operation.

in

Used in a foreach statement to specify the collection you want to iterate through.

int

Specifies a data type that represents a signed 32-bit integer number. It can represent a number from −2,147,483,648 to 2,147,483,647.

interface

Used to define an interface, which is a definition that contains the signatures of methods, delegates, and events. An interface does not contain any implementation.

internal

An access modifier to indicate a member that can only be accessed within files in the same assembly.

is

Used to check if an object is compatible with a given type.

lock

Marks a statement block as a critical section so that other threads cannot execute the block while the statements within the block are being executed.

long

Specifies a data type that represents a signed 64-bit integer number. It can represent a number from −9,223,372,036,854,775,808 to 9,223,372,036,854,775,807.

namespace

Used to organize your code so that it belongs to a globally unique type.

new

Used to create objects and invoke a class's constructor. Also can be used to explicitly hide a base class's member in a derived class. When used in a generic declaration, it restricts types that might be used as arguments for a type declaration.

null

Represents a null reference that does not refer to any object.

object

A C# alias of the System.Object .NET Framework type.

operator

Used to overload a built-in operator or provide a conversion operator.

out

Indicates arguments that are to be passed by reference. It is similar to ref, except that ref requires the variable to be initialized before it is passed.

override

Extends or modifies the abstract or virtual implementation of an inherited method, property, indexer, or event.

params

Specifies a parameter array where the number of arguments is variable.

private

An access modifier used to indicate a member that can only be accessed within the body of the class or struct in which it's declared.

protected

An access modifier used to indicate a member that can only be accessed within its class and derived classes.

public

An access modifier used to indicate a member that can be accessed by all code.

readonly

A modifier that indicates fields that can only be initialized at declaration or in a constructor.

ref

Indicates arguments that are to be passed by reference.

return

Terminates execution of a method and returns control to the calling method.

sbyte

Specifies a data type that represents a signed 8-bit integer number. It can represent a number from −128 to 127.

sealed

Specifies a class that does not allow other classes to derive from it.

short

Specifies a data type that represents a signed 16-bit integer number. It can represent a number from −32,768 to 32767.

sizeof

Used to obtain the size in bytes for a value type.

stackalloc

Used in an unsafe code context to allocate a block of memory on the stack.

static

A modifier to indicate that a member belongs to the type itself, and not to a specific object.

string

Specifies a data type that represents a sequence of zero or more Unicode characters. Also an alias for the System.String .NET Framework type.

struct

Denotes a value type that encapsulates a group of related variables.

switch

A control statement that handles multiple selections by matching the value of the switch with a series of case statements.

this

Refers to the current instance of the class. Also used as a modifier of the first parameter of an extension method.

throw

Used to invoke an exception during runtime.

true

Used either as an operator or as a literal. One of the possible values in a bool variable.

try

Indicates a block of code that may cause exceptions. Used with one or more catch blocks to handle the exceptions raised.

typeof

Used to obtain the System.Type object for a type.

uint

Specifies a data type that represents an unsigned 32-bit integer number. It can represent a number from 0 to 4,294,967,295.

ulong

Specifies a data type that represents an unsigned 64-bit integer number. It can represent a number from 0 to 18,446,744,073,709,551,615.

unchecked

Used to suppress overflow-checking for integral-type arithmetic operations and conversions.

unsafe

Denotes an unsafe context, which is required for any operation involving pointers.

ushort

Specifies a data type that represents an unsigned 16-bit integer number. It can represent a number from 0 to 65,535.

using

A directive for creating a namespace alias or importing namespace references. It is also used for defining a scope at the end of which an object will be disposed.

virtual

An access modifier to indicate a method, property, indexer, or event declaration and allow for it to be overridden in a derived class.

volatile

Indicates that a field might be modified by multiple threads that are executing at the same time.

void

Specifies that a method does not return any value.

while

Executes a statement or a block of statements until a specified expression evaluates to false.

Contextual Keywords

The following table describes the context keywords used in C#.

Keyword

Description

from

Used in a LINQ query. A query expression must begin with a from clause.

get

Defines an accessor method in a property or indexer. It retrieves the value of the property or indexer element.

group

Used in a LINQ query and returns a sequence of IGrouping<(Of <(TKey, TElement>)>) objects that contain zero or more items that match the key value for the group.

into

Used in a LINQ query and can be used to create a temporary identifier to store the results of a group, join, or select clause into a new identifier.

join

Used in a LINQ query for associating elements from different sources.

let

Used in a LINQ query to store the result of a subexpression to be used in a subsequent clause.

orderby

Used in a LINQ query to sort the result of a query in either ascending or descending order.

partial

Denotes that the definition of a class, struct, or interface is split into multiple files. Also denotes that a method's signature is defined in one partial type and its definition is defined in another partial type.

select

Used in a LINQ query to specify the type of values that will be produced when the query is executed.

set

Defines an accessor method in a property or indexer. It assigns a value to the property or indexer element.

value

An implicit parameter in a set accessor. It is also used to add or remove event handlers.

where

Used in a LINQ query to specify which elements from the data source will be returned in the query expression.

yield

Used in an iterator block to provide a value to the enumerator object or to signal the end of iteration.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.22.74.160