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.
| |||
| |||
|
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.
Concepts
Other Resources
|
Date |
History |
Reason |
|---|---|---|
|
July 2009 |
Added add, remove, and global to the contextual keywords. |
Information enhancement. |
CS Team: Both ascending gnad descending have been added in later versions of the documentation. Thanks.
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.)
More about undocumented keywords __makeref, __reftype, __refvalue and __arglist http://www.eggheadcafe.com/articles/20030114.asp .
__makeref
Creates typed reference from a variable.int i = 1;
TypedReference tr = __makeref(i);
__reftype
Extracts original type of the variable represented by typed reference.int i = 1;
TypedReference tr = __makeref(i);
Type t = __reftype(tr);
__refvalue
Extracts original value from typed reference.int i = 1;
TypedReference tr = __makeref(i);
int j = __refvalue(tr, int);
__arglist
Used to work with argument lists (arglists) on methods. Arglist is very similar to params keyword but it is more low-level. While params relies mainly on compiler, arglist has specific support in CIL.The __arglist keyword can be used to specify method with variable argument list, to pass arguments to such method and to access arguments inside the method.
void AcceptsArguments(__arglist){
ArgIterator ai = new ArgIterator(__arglist);
List<object> list = new List<object>();
while(ai.GetRemainingCount() > 0){
TypedReference tr = ai.GetNextArg();
list.Add(TypedReference.ToObject(tr));
}
}Usage:
int a = 3;
string b = "3";
char c = '3';
AcceptsArguments(__arglist(a,b,c));
CS Team: What counts as a keyword is defined in the C# Language Specification (http://www.microsoft.com/downloads/details.aspx?FamilyID=dfbf523c-f98c-4804-afbd-459e846b268e&displaylang=en#filelist). Keywords add, remove, ascending, descending, extern, and alias have already been added to later versions of the documentation (http://msdn.microsoft.com/en-us/library/x53a06bb.aspx). Thanks.
(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 way 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;
}
}
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);