Code snippets are ready-made snippets of code you can quickly insert into your code.
Eg: snippet for Console.WriteLine, just type:
cw and then tab tab.
Eg2: insert class property, just type:
prop and then tab tab.
Outputs:
Eg3: insert incrementing loop, just type:
forr and then tab tab
Outputs:
Eg4: creating constructor and creating a private field automatically:
To create constructor automatically, type:
ctor + tab + tab
To create a private field automatically, type:
ctrl + .
And then press enter on the line of code that you want to use from a list of available options:
Eg: generating a constructor and private field shortcuts:
Default code snippets:
By default the following code snippets are included in visual studio for c sharp:
Name (or shortcut) Description Valid locations to insert snippet
cw Creates a call to WriteLine. Inside a method, an indexer
, a property accessor
, or an event accessor.
if Creates an if block. Inside a method
, an indexer
, a property accessor
, or an event accessor.
else Creates an else block. Inside a method
, an indexer
, a property accessor
, or an event accessor.
switch Creates a switch block. Inside a method
, an indexer
, a property accessor
, or an event accessor.
while Creates a while loop. Inside a method
, an indexer
, a property accessor
, or an event accessor.
for Creates a for loop. Inside a method
, an indexer
, a property accessor
, or an event accessor.
foreach Creates a foreach loop. Inside a method
, an indexer
, a property accessor
, or an event accessor.
forr Creates a for loop that decrements Inside a method
the loop variable after each iteration. , an indexer
, a property accessor
, or an event accessor.
do Creates a do while loop. Inside a method
, an indexer
, a property accessor
, or an event accessor.
#region Creates a #region directive and a #endregion directive. Anywhere.
#if Creates a #if directive and a #endif directive. Anywhere.
~ Creates a finalizer (destructor) for the containing class. Inside a class.
checked Creates a checked block. Inside a method
, an indexer
, a property accessor
, or an event accessor.
class Creates a class declaration. Inside a namespace
(including the global namespace)
, a class, or a struct.
prop Creates an auto-implemented property declaration. Inside a class or a struct.
propfull Creates a property declaration with get and set accessors. Inside a class or a struct.
propg Creates a read-only auto-implemented property Inside a class or a struct.
with a private set accessor.
ctor Creates a constructor for the containing class. Inside a class.
indexer Creates an indexer declaration. Inside a class or a struct.
equals Creates a method declaration that overrides Inside a class or a struct.
the Equals method defined in the Object class.
attribute Creates a declaration for a class that derives from Attribute. Inside a namespace
(including the global namespace)
, a class
, or a struct.
enum Creates an enum declaration. Inside a namespace
(including the global namespace)
, a class
, or a struct.
struct Creates a struct declaration. Inside a namespace
(including the global namespace)
, a class
, or a struct.
using Creates a using directive. Inside a namespace
(including the global namespace).
namespace Creates a namespace declaration. Inside a namespace
(including the global namespace).
try Creates a try-catch block. Inside a method
, an indexer
, a property accessor
, or an event accessor.
tryf Creates a try-finally block. Inside a method
, an indexer
, a property accessor
, or an event accessor.
exception Creates a declaration for a class Inside a namespace
that derives from an exception (Exception by default). (including the global namespace)
, a class
, or a struct.
interface Creates an interface declaration. Inside a namespace
(including the global namespace)
, a class
, or a struct.
invoke Creates a block that safely invokes an event. Inside a method
, an indexer
, a property accessor
, or an event accessor.
iterator Creates an iterator. Inside a class or a struct.
iterindex Creates a "named" iterator Inside a class or a struct.
and indexer pair by using a nested class.
lock Creates a lock block. Inside a method
, an indexer
, a property accessor
, or an event accessor.
mbox Creates a call to System.Windows.Forms.MessageBox.Show. Inside a method
You may have to add a reference to System.Windows.Forms.dll. , an indexer
, a property accessor
, or an event accessor.
sim Creates a static int Main method declaration. Inside a class or a struct.
svm Creates a static void Main method declaration. Inside a class or a struct.
unchecked Creates an unchecked block. Inside a method
, an indexer
, a property accessor
, or an event accessor.
unsafe Creates an unsafe block. Inside a method
, an indexer
, a property accessor
, or an event accessor.
For more information, see here.