O'Reilly logo

C# 2008 Programmer's Reference by Wei-Meng Lee

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required