#981 – Full List of C# Keywords

In C#, identifiers are names that you choose to use for things like variables, classes, interfaces, etc.

A keyword is a reserved name that has a specific meaning and that you can’t (generally) use as an identifier.

Here’s the full list of standard keywords in C#:

 abstract  add  as  ascending
 async  await  base  bool
 break  by  byte  case
 catch  char  checked  class
 const  continue  decimal  default
 delegate  descending  do  double
 dynamic  else  enum  equals
 explicit  extern  false  finally
 fixed  float  for  foreach
 from  get  global goto
group  if  implicit  in
 int  interface  internal  into
 is  join  let  lock
 long  namespace  new  null
 object  on  operator  orderby
 out  override  params  partial
 private  protected  public  readonly
 ref  remove  return  sbyte
 sealed  select  set  short
 sizeof  stackalloc  static  string
 struct  switch  this  throw
 true  try  typeof  uint
 ulong  unchecked  unsafe  ushort
 using  value  var  virtual
 void  volatile  where  while
 yield      
Advertisements

#959 – Don’t Use Double Underscores at the Start of an Identifier

When naming identifiers in C#, you can use the underscore (‘_’) character anywhere in the identifier name, including at the beginning of the name.

Some people use identifiers that begin with a single underscore for private fields (e.g. _name_age).  This use is no longer recommend.  You should instead just use camelCasing for private fields.  If you want to make clear that the identifer is a member variable, you can use the this keyword (e.g. this.namethis.age).

You should, however, never use a double underscore at the start of an identifier name.  There are already several reserved keywords that start with a double underscore (e.g. __reftype, __refvalue).  The double underscore notation is meant to be used for these reserved keywords and future revisions to C# may add new keywords that may then conflict with any identifiers that you have in your code.

#476 – Don’t Use Unicode Escape Sequences in Identifiers

Although you can embed Unicode escape sequences within identifiers in a C# program, you shouldn’t.  Including escape sequences makes the identifiers in the code less readable.

You can include Unicode characters for any Unicode character in the range of U+0000 to U+FFFF and the glyph will show up correctly in Visual Studio, if you have the proper fonts installed.  You can also enter these characters directly using the appropriate international keyboard.

In  the example below, we declare an identifier that includes the letter ‘c’ with a “cedilla”, using the Unicode value of U+00E7 for the cedilla.

            string fa\u00E7ade = "Front of building";

This is valid C# code, but should be avoided. Instead, you can insert the glyph directly into the code:

            string façade = "Front of building";

Visual Studio treats these as identical. We get a compiler warning if we include both definitions in a program.

#19 – Contextual Keywords

There are some keywords in C# that are considered contextual, in that their usage is determined by their context.  Technically, you are free to use these keywords as identifiers, without needing the ‘@’ symbol.  But you should avoid doing this, since it could lead to confusion.

The contextual keywords are:  add, dynamic, get, global, partial, remove, set, value, var, where, yield

string var = "Don't do this, either";

#18 – What Is an Identifier?

An identifier is any name that you choose to use in a C# program for things like: variables, classes, interfaces, methods, properties, etc.

The rules for naming identifiers include:

  • They are case sensitive  (i.e. “name” is different from “Name”)
  • They must begin with a letter or underscore (‘_’)
  • They cannot include spaces
  • They can include Unicode characters
  • They cannot match any of the built-in C# keywords

You can actually name an identifier with a name matching a built-in keyword if you prefix the identifer with the ‘@’ symbol.  In the following example, we declare a variable of type string and name the variable “string”.  This is legal in C#, but not a good practice.

string @string = "Don't do this";