C# Keywords Home
This page is specific to:.NET Framework Version:1.12.03.03.54.0
C# Language Reference
C# Keywords

Updated: July 2009

Keywords are predefined, reserved identifiers that have special meanings to the compiler. They cannot be used as identifiers in your program unless they include @ as a prefix. For example, @if is a valid identifier but if is not because if is a keyword.

The first table in this topic lists keywords that are reserved identifiers in any part of a C# program. The second table in this topic lists the contextual keywords in C#. Contextual keywords have special meaning only in a limited program context and can be used as identifiers outside that context. Generally, as new keywords are added to the C# language, they are added as contextual keywords in order to avoid breaking programs written in earlier versions.

Contextual Keywords

A contextual keyword is used to provide a specific meaning in the code, but it is not a reserved word in C#. Some contextual keywords, such as partial and where, have special meanings in two or more contexts.

See Also

Concepts

Other Resources

Change History

Date

History

Reason

July 2009

Added add, remove, and global to the contextual keywords.

Information enhancement.

Community Content

Event Delegates Add and Remove Accessor Methods
Added by:Akidan

(add and remove actually are listed above. It's under "Contextual Keywords")

The add and remove accessor methods (similar to the get and set for a property) are not included in the list above.

These allow you to intercept the calls when others wire up to your events in much the same was as get/set allows you to intercept calls to a property.

Below is some sample code that shows the accessor functions.

public event EventHandler OnClick
{
add {
Debug.WriteLine("add called");
Click += value;
}
remove {
Debug.WriteLine("remove called");
Click -= value;
}
}
Descending is missing from the list of Contextual Keywords
Added by:Rick Strahl
The descending keyword (as in orderby ... descending) is not listed above.
Missing Keywords
Added by:gäst
There's actually a bunch of keywords missing!

As mentioned before, there are add and remove, and, of coure, descending and (!) ascending!

In addition to this, I miss the context-based alias-Keyword (as in extern alias).

Then, if I may ask, why don't you document the __makeref, __reftype, __refvalue und __arglist keywords?

And last but not least, though they are not really "keywords", I think that attribute path specifiers would fit here (assembly, module, method etc.)


Why the length is 14 & 15?
Added by:Stryker

using System;

class Myclass
{
static void Main()
{
//int[] iPages = { 5,345345 };
//Console.WriteLine(iPages.ToString().Length); => This will output 14

//string[] iPages = { "5","adf","345345" };
//Console.WriteLine(iPages.ToString().Length); => This will output 15
}

}

I'm diving with C# data types, can someone please explain why the results are 14, 15 always?

Reply: If you view the iPages.ToString() you will get something like "System.String[]" Or "System.Int32[]" it doesn't actually print what is contained in the array

Comment:

int intLength = "System.Int32[]".Length;
Assert.IsTrue(intLength==14);

© 2009 Microsoft Corporation. All rights reserved.   Terms of Use | Trademarks | Privacy Statement
Page view tracker
Rate the Lightweight library
x
Lightweight builds on ScriptFree (loband) by adding features you've requested: a SearchBox and default code language selection.
Do you like the SearchBox?
Do you like the tabbed code blocks?
How useful is this topic?
Tell us more.
Thanks
x
You're helping to improve MSDN Online.
Feedback
Switch View
Classic
Lightweight Beta
ScriptFree
Switch View