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 follow­ing 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
Вы читаете C# 2008 Programmer's Reference
Добавить отзыв
ВСЕ ОТЗЫВЫ О КНИГЕ В ИЗБРАННОЕ

0

Вы можете отметить интересные вам фрагменты текста, которые будут доступны по уникальной ссылке в адресной строке браузера.

Отметить Добавить цитату