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 ...

Get C# 2008 Programmer's Reference now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.