Tuesday 16 August 2011

VisualStudio: Cheat Sheet CSharp Code Snippets


Question:
What are the common c# code snippets in Visual Studio 2010.


Answer:

The c# snippets are installed here: "$VSInstallPath$\VC#\Snippets\1036\Visual C#"
On my machine it is : "C:\Program Files\Microsoft Visual Studio 10.0\VC#\Snippets\1036\Visual C#"

Others like asp.net are in folders near by.
The asp.net snippets are here: $VSInstallPath$\Web\Snippets\HTML\1036\ASP.NET

You can find a cheat sheet for c# code snippets on John Sheehan's blog.

Shortcut for CodeSnippets: Ctrl-K + Ctrl-X

Auto-completion: Place cursor, type shortcut, press TAB

Here is my version with the snippet code

Shortcut Description Type Code
#if #if/#endif directive E, S
#if $expression$
    $selected$ $end$ 
#endif
#region #region/#endregion directive E, S
#region $name$
    $selected$ $end$
#endregion
~ destructor E
~$classname$()
{
    $end$
}
Attribute attribute (using recommended pattern) E
checked checked block E, S
checked
{
    $selected$ $end$
}
class class declaration E, S
class $name$
{
    $selected$$end$
}
ctor constructor E
public $classname$ ()
{
    $end$
}
cw Console.WriteLine E
$System.Console$.WriteLine($end$);
do do...while loop E, S
do
{
    $selected$ $end$
} while ($expression$);
else else statement E, S
else
{
    $selected$ $end$
}
enum enum declaration E, S
enum $name$
{
    $selected$ $end$
}
equals implementing Equals() according to guidelines E
Exception exception E
[$System.Serializable$]
public class $newException$Exception : $baseException$
{
    public $newException$Exception() { }
    public $newException$Exception( string message ) : base( message ) { }
    public $newException$Exception( string message, $SystemException$ inner ) : base( message, inner ) { }
    protected $newException$Exception( 
        $SystemRuntimeSerializationSerializationInfo$ info, 
        $SystemRuntimeSerializationStreamingContext$ context ) : base( info, context ) { }
}
for 'for' loop E, S
for (int $index$ = 0; $index$ < $max$; $index$++)
{
    $selected$ $end$
}
foreach foreach statement E, S
foreach ($type$ $identifier$ in $collection$)
{
    $selected$ $end$
}
forr reverse 'for' loop E, S
for (int $index$ = $max$ - 1; $index$ >= 0 ; $index$--)
{
    $selected$ $end$
}
if if statement E, S
if ($expression$)
{
    $selected$ $end$
}
indexer indexer declaration E
$access$ $type$ this[$indextype$ index]
{
    get {$end$ /* return the specified index here */ }
    set { /* set the specified index to value here */ }
}
interface interface declaration E, S
interface I$name$
{
    $selected$$end$
}
invoke safely invoking an event E
$DelegateType$ temp = $event$;
if (temp != null)
{
    temp($end$);
}
iterator simple iterator E
public $System.Collections.Generic.IEnumerator$<$type$> GetEnumerator()
{
    $end$throw new $Exception$();
    yield return default($type$);
}
iterindex 'named' iterator/indexer pair using a nested class E
lock lock block E, S
lock ($expression$)
{
    $selected$ $end$
}
mbox MessageBox.Show (requires reference to System.Windows.Forms) E
$System.Windows.Forms.MessageBox$.Show($string$);$end$
namespace namespace declaration E, S
namespace $name$
{
    $selected$ $end$
}
prop automatically implemented property E, V3+
public $type$ $property$ { get; set; }$end$
propfull property and backing field E
private $type$ $field$;

public $type$ $property$
{
    get { return $field$;}
    set { $field$ = value;}
}
$end$
propg autoproperty with a 'get' accessor and a private 'set' accessor E, V3+
public $type$ $property$ { get; private set; }$end$
sim 'static int Main()' method declaration E
static int Main(string[] args)
{
    $end$
    return 0;
}
struct struct declaration E, S
struct $name$
{
    $selected$$end$
}
svm 'void Main' method declaration E
static void Main(string[] args)
{
    $end$
}
switch switch statement block E
switch ($expression$)
{
    $cases$
}$end$
testc MSTest test class E
testm MSTest test method E
try try catch block E, S
try 
{         
    $selected$
}
catch ($expression$)
{
    $end$
    throw;
}
tryf try finally block E, S
try 
{         
    $selected$
}
finally
{
    $end$
}
unchecked unchecked block E, S
unchecked
{
    $selected$ $end$
}
unsafe unsafe block E, S
unsafe
{
    $selected$ $end$
}
using using block E, S
using($resource$)
{
    $selected$ $end$
}
while while loop E, S
while ($expression$)
{
    $selected$ $end$
}

No comments: