Friday, November 16, 2007

C# Syntax Cheat Sheet


x       Indicates x is supplied by the programmer or to be used verbatim
x?      Indicates x may occur zero-or-one times
x*      Indicates x may occur zero-or-more times, separated by commas
x+      Indicates x may occur one-or-more times,  separated by commas
[...]   Indicates a logical grouping of code elements, when not implicitly grouped using the verbatim 
terms {}, (), and []
[x|y]   Indicates only one of a choice of code elements may occur


 


Expression Statements


[variable = ]? expression;


 


Variable declaration        


type [variable [ = expression ]?]+ ;


 


Checked/unchecked operator


checked (expression)


unchecked (expression)


 


Checked/unchecked statement


Checked  statement-or-statement-block


Unchecked statement-or-statement-block


 


new operator 


class-var = new class-name();


 


Constant declaration       


const type [variable = constant-expression]+ ;


 


if-else statement


if (Boolean-expression)


statement-or-statement-block [


else


statement-or-statement-block ]?


 


switch statement


switch (expression) {


[ case constant-expression : statement* ]*


[ default : statement* ]? }


 


while loops


while (Boolean-expression) statement-or-statement-block


 


do-while loops


do


statement-or-statement-block while (Boolean-expression);


 


 


for loops


for (statement?; Boolean-expression?; statement?)


statement-or-statement-block


 


foreach loops


foreach ( type-value in IEnumerable) statement-or-statement-block


or


foreach(type var-name in collection) statement


 


goto statement :


goto statement-label;


goto case-constant


 


return statement


return expression?;


 


throw statement     


throw exception-expression?;


or


throw exceptOb;


The exceptOb must be an object of an exception class derived from Exception.


 


lock statement


lock (expression) statement-or-statement-block


 


using statement


using (declaration-expression) statement-or-statement-block


 


 


Namespace declaration syntax:


namespace name+ {


   using-statement*


   [namespace-declaration | type-declaration]*


}


 


Class


attributes? unsafe? access-modifier?


new?


[ abstract | sealed ]?   


class class-name [: base-class | : interface+ |: base-class, interface+ ] ?


{ class-members }


 


 


interface declaration:


interface name {


  ret-type method-name1(param-list);


  ret-type method-name2(param-list);


  // ...


  ret-type method-nameN(param-list);


}


 


interface property


type name {


  get;


  set;


}


 


interface indexer


element-type this[int index] {


  get;


  set;


}


 


Fields


attributes? unsafe? access-modifier? new?


static?


[readonly | volatile]?


type [ field-name [ = expression]? ]+ ;


 


 


Struct


attributes? unsafe? access-modifier?


new?


struct struct-name [: interface+]?


{ struct-members }


 


  


Constant 


attributes? access-modifier?


new?


const type [ constant-name = constant-expression ]+;


 


 


Properties


attributes? unsafe? access-modifier?


[


[[sealed | abstract]? override] |    


new? [virtual | abstract | static]?


]?


type property-name { [    


attributes? get statement-block |    //     read-only


attributes? set statement-block |    //      write-only


attributes? get statement-block      //     read-write


attributes? set statement-block ] 


}


 


Indexers


attributes? unsafe? access-modifier?             


[                 


  [[sealed | abstract]? override] |           


  new? [virtual | abstract]?            


]?               


type this [ attributes? [type arg]+ ]      {       


attributes? get statement-block |  //       read-only


attributes? set statement-block |  //       write-only


attributes? get statement-block    //       read-write


attributes? set statement-block             


}


 


Method


attributes? unsafe? access-modifier? [


[[sealed | abstract]? override] |


new? [ virtual | abstract ]?


[ void | type ] method-name (parameter-list) statement-block


 


 


Parameter list


[attributes? [ref | out]? type arg ]* [ params attributes? type[ ] arg ]?


 


 


Instance Constructors


attributes? unsafe? access-modifier?


class-name (parameter-list) [ :[ base | this ] (argument-list) ]?


statement-block


 


calling base class contructor :


derived-constructor(parameter-list) : base(arg-list) {


   // body of constructor


}


 


Static Constructors


attributes? unsafe? extern? static class-name ( ) statement-block


 


Destructors and Finalizers


attributes? unsafe? ~class-name () statement-block


 


Interfaces


attributes? unsafe? access-modifier? new?


interface interface-name [ : base-interface+ ]? { interface-members }


 


 


Arrays


type[*]+ array-name = new type [ dimension+ ][*]*;


 


[*] is the set: [] [,] [,,]


 


Initializing a one-dimensional array


type[ ] array-name = { val1, val2, val3, ..., valN };


 


 


Enums


attributes? access-modifier?


new?


enum enum-name [ : integer type ]?


{ [attributes? enum-member-name [ = value ]? ]* }


 


 


Delegates


attributes? unsafe? access-modifier? new?


Delegate [ void | type ]  delegate-name (parameter-list);


 


Delegate-invocations


primary-expression-of-a-delegate-type (expression);


 


Delegate-creation


new   delegate-type  (expression);


The argument of a delegate creation expression must be a method group or a value of a delegate-type.


 


Events


access-modifier? event event-delegate object-name;


 


Event Accessors


 attributes? unsafe? access-modifier?


[


  [[sealed | abstract]? override] | new? [virtual | static]?


]?


event delegate type event-property-accessor-name


{


attributes? add statement-block


attributes? remove statement-block


}


 


 


try statement


try statement-block


[catch (exception type value?)? statement-block]+  | finally statement-block |


[catch (exception type value?)? statement-block]+     finally statement-block


 


 


Attributes


[[target:]? attribute-name


(


positional-param+ |


[named-param = expression]+ |


positional-param+, [named-param = expression]+)?]


 


overloading a unary operator


public static ret-type operator op(param-type operand)


{


   // operations


}


 


overloading a binary operator


public static ret-type operator op(param-type1 operand1, param-type1 operand2)


{


   // operations


}


 


 


NET-compatible event handlers 


void handler(object source, EventArgs arg) {


  // ...


}


 


Delegates


public delegate  void  Del<T>(T item);


public void Notify(int i) { }


 


Del<int> d1 = new Del<int>(Notify);


 


In C# 2.0


 


Del<int> d2 = Notify;


 


 


generic class


class class-name<type-param-list> { // ...


 


declaring a reference to a generic class


class-name<type-arg-list> var-name =


new class-name<type-arg-list>(cons-arg-list);


 


generic method


ret-type meth-name<type-parameter-list>(param-list) { // ...


 


generic delegate


delegate ret-type delegate-name<type-parameter-list>(arg-list);


 


 


References :
C# Essentials, 2nd Edition, Ben Albahari Peter Drayton Brad Merrill
Microsoft Visual C# .NET,
http://msdn.microsoft.com/vcsharp
ECMA C# Standard, http://www.ecma.ch/ecma1/stand/ecma-334.htm


 

No comments:

Indonesia To Blog -Top Site
SEO - search engine submission and optimisation