views:

596

answers:

5

I was wondering if there exists somewhere a collection or list of C# syntax shortcuts. Things as simple omitting the curly braces on if statements all the way up to things like the ?? coalesce operator.

A: 

I don't know of a precompiled list, but the C# Reference (especially the C# Keywords section) concisely contains the information you're looking for if you're willing to read a bit.

Joren
+1  A: 

How does this C# basic reference pdf document looks to you?

Here's another pdf.

Vadim
Thanks, I've seen the first one and it has a few examples on there, I was just wondering if there existed a more comprehensive list out there, including some of the not so obvious ones.
Graham
+1  A: 
a = b ? c : d ;

is short for

if (b) a = c; else a = d;

And

int MyProp{get;set;}

is short for

int myVar;
int MyProp{get {return myVar; } set{myVar=value;}}

Also see the code templates in visual studio which allows you to speed coding up.

But note that short code doesn't mean necessarily good code.

codymanix
+1  A: 

My all time favorite is

a = b ?? c;

which translates to

if (b != null) then a = b; else a = c;
mr_dunski
actually, that would be if b is not null, then a equals b, else a equals c
Rake36
heh correct :)
mr_dunski
A: 

They are not syntax shortcuts, but snippets are great coding shortcuts. Typing prop (tab)(tab), for example, stubs out the code you need for a property.

http://msdn.microsoft.com/en-us/library/ms165392%28VS.80%29.aspx

HeathenWorld