The following table describes the syntax of various C# language elements. The left column shows the syntax for each element, and the right column includes one or more examples.
Arrays | ||
type [*] + array-name = [ new type [ dimension+ ][*]*; | { value1, value2, ... }; ] |
byte[ ] arr1 = new byte[10]; int[ ] arr2 = {0, 1, 2}; ([*] is the set: [ ] [,] [,,] etc.) | |
Attributes | ||
[[target:]? attribute-name ( positional-param+ | [named-param = expr]+ | positional-param+, [named-param = expr]+)?] |
[assembly:CLSCompliant(false)] [WebMethod(true, Description="My web method")] | |
Break statement | ||
break; |
break; | |
Checked/unchecked | ||
checked (expr) unchecked (expr) |
// throws exception short x = 32767; int i = checked( (short) ++x ); // silently overflows to -32768 short y = 32767; int j = unchecked( (short) ++y ); | |
checked [statement | statement-block] unchecked [statement | statement-block] |
// throws exception public short foo( ) { short y = 32767; checked { return ++y; } } // silently overflows public short bar( ) { short y = 32767; unchecked { return ++y; } } | |
Class declaration | ||
attributes? unsafe? access-modifier? new? [ abstract | sealed ]? class class-name [: base-class | : interface+ | : base-class, interface+ ]? { class-members } |
public class MyClass : Base, IFoo { // ... } | |
Constant declaration | ||
const type [variable = constant-expr]+; |
const int xyzzy = 42; | |
Constant fields | ||
attributes? access-modifier? new? const type [constant-name = constant-expr]+; |
internal const byte fnord = 23; | |
Continue statement | ||
continue; |
continue; | |
Delegates | ||
attributes? unsafe? access-modifier? new? delegate [ void | type ] delegate-name (parameter-list); |
public delegate void MyHandler(object s, EventArgs e); | |
Destructors | ||
attributes? unsafe? ~class-name ( ) statement-block |
~SomeClass( ) { // destructor code } | |
Do-While loops | ||
do [statement |statement-block] while (Boolean-expr); |
int i = 0; do { // print 0 through 9 Console.WriteLine(i++); } while(i < 10); | |
Empty statements | ||
; |
i = 0; while(i++ < 10) ; // take no action Console.WriteLine(i); // prints 11 | |
Enums | ||
attributes? access-modifier? new? enum enum-name [ : integer-type ]? { [attributes? enum-member-name [ = value ]? ]* } |
[Flags] public enum Color : long { Red = 0xff0000, Green = 0x00ff00, Blue = 0x0000ff }; //... // prints "Green, Red" Color yellow = (Color) 0xffff00; Console.WriteLine(yellow); | |
Events | ||
attributes? unsafe? access-modifier? [ [[sealed | abstract]? override] | new? [virtual | static]? ]? event delegate-type event-name |
event MyDelegate OnClickedSomething; // ... OnClickedSomething(arg1, arg2); | |
Event accessors | ||
attributes? unsafe? access-modifier? [ [[sealed | abstract]? override] | new? [virtual | static]? ]? event delegate-type event-accessor-name { attributes? add statement-block attributes? remove statement-block } |
event MyDelegate OnAction { add { // ... } remove { // ... } } | |
Expression statements | ||
[variable =]? expr; |
a = 10 * 10; a++; b = ++a; | |
Fields | ||
attributes? unsafe? access-modifier? new? static? [readonly | volatile]? type [ field-name [ = expr]? ]+ ; |
protected int agent = 0x007; | |
Fixed statements | ||
fixed ([value-type | void ]* name = [&]? expr ) statement-block |
byte[ ] b = {0, 1, 2}; fixed (byte* p = b) { *p = 100; // b[0] = 100 } | |
For loops | ||
for (statement?; Boolean-expr?; statement?) [statement | statement-block] |
// print 0 through 9 for(int j=0; j<10; j++) Console.WriteLine(j); | |
Foreach loops | ||
foreach ( type-value in IEnumerable ) statement or statement-block |
StringCollection sc = new StringCollection( ); sc.Add("Hello"); sc.Add("World"); foreach(String s in sc) Console.WriteLine(s); | |
Goto statement | ||
goto statement-label; goto case-constant; |
i = 0; MyLabel: if(++i < 100) goto MyLabel; Console.WriteLine(i); | |
If-Else statement | ||
if (Boolean-expr) [statement | statement-block] [ else [statement | statement-block] ]? |
if(choice = = "A") { // ... } else if (choice = = "B") { // ... } else { // ... } | |
Indexers | ||
attributes? unsafe? access-modifier? [ [[sealed | abstract]? override] | new? [virtual | abstract | static]? ]? type this [ attributes? [type arg]+ ] { attributes? get // read-only statement-block | attributes? set // write-only statement-block | attributes? get // read-write statement-block attributes? set statement-block } |
string this[int index] { get { return somevalue; } set { // do something with // implicit "value" arg } } | |
Instance constructors | ||
attributes? unsafe? access-modifier? class-name (parameter-list) [ :[ base | this ] (argument-list) ]? statement-block |
MyClass(int i) { // perform initialization } // Initialize with default MyClass( ) : this(42) { } | |
Interfaces | ||
attributes? unsafe? access-modifier? new? interface interface-name [ : base-interface+ ]? { interface-members } |
interface IFoo : IDisposable, IComparable { // member declarations } | |
Lock statement | ||
lock (expr) [statement | statement-block] |
lock(this) { int tmp = a; a = b; b = tmp; } | |
Method declaration syntax | ||
attributes? unsafe? access-modifier? [ [[sealed | abstract]? override] | new? [virtual | abstract | static extern?]? ]? [ void | type ] method-name (parameter-list) statement-block |
public abstract int MethA(object o); public virtual void MethB(int i, object o) { // statements... } | |
Namespace | ||
namespace name+ { using-statement* [namespace-declaration | type-declaration]* } (namespace is dot-delimited) (namespace-declaration has no delimiters) |
namespace OReilly.CSharp { using System; interface IFoo : IComparable { } public class MyClass { } } | |
Parameter list | ||
[ attributes? [ref | out]? type arg ]* [ params attributes? type[ ] arg ]? |
void MethA(ref int a, out int b) { b = ++a; } void MethB(params string[ ] args) { foreach (string s in args) Console.WriteLine(s); } // ... int a = 20, b; MethA(ref a, out b); Console.WriteLine("a={0}, b={1}", a, b); MethB("hello", "world"); | |
Properties | ||
attributes? unsafe? access-modifier? [ [[sealed | abstract]? override] | new? [virtual | abstract | static]? ]? type property-name { [ attributes? get // read-only statement-block | attributes? set // write-only statement-block | attributes? get // read-write statement-block attributes? set statement-block ] } |
private string name; public string Name { get { return name; } set { name = value; } } | |
Return statement | ||
return expr?; |
return; return x; | |
Statements and statement blocks | ||
statement |
int x = 100; | |
statement-block |
{ int x = 100; Console.WriteLine(x); return x; } | |
Static constructors | ||
attributes? unsafe? extern? static class-name ( ) statement-block |
static MyClass( ) { // initialize static members } | |
Struct declaration | ||
attributes? unsafe? access-modifier? new? struct struct-name [: interface+]? { struct-members } |
public struct TwoFer { public int part1, part2; } | |
Switch statement | ||
switch (expr) { [ case constant-expr : statement* ]* [ default : statement* ]? } |
switch(choice) { case "A": // ... do something break; case "B": // ... do something // then branch to A goto case "A"; case "C": case "D": // ... do something break; default: Console.WriteLine("bad choice"); break; } | |
Throw statement | ||
throw exception-expr?; |
throw new Exception("something's wrong"); | |
Try statements and exceptions | ||
try statement-block [catch (exception type value?)? statement-block]+ | finally statement-block | [catch (exception type value?)? statement-block]+ finally statement-block |
try { // do something } catch (Exception) { // recover } finally { // this will always // be called } | |
Using statement | ||
using (declaration-expr) [statement | statement-block] |
using(StreamReader s = new StreamReader("README.TXT")) { // ... } // s is disposed here | |
Variable declaration | ||
type [variable [ = expr ]?]+ ; |
long a, b, c; int x = 100; | |
While loops | ||
while (Boolean-expr) [statement | statement-block] |
int i = 0; while(i < 10) { // print 0 through 9 Console.WriteLine(i++); } |