C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Tip: The snippet inserts a parameterless public constructor, which is probably the most common type.
ConstructorHowever: If you want to add parameters, simply add them inside the parentheses manually.
Info: If you want multiple constructors, just add more in the same way with this snippet.
Where to use ctor snippet: press tab tab
class Example
{
ctor
}
class Program
{
static void Main()
{
}
}
Output
class Example
{
public Example()
{
}
}
class Program
{
static void Main()
{
}
}
Tip: You can use the class snippet anywhere in a file, but you should only use it in a place where a class can be declared.
Info: The class snippet allows you to instantly insert the class definition with correct syntax in your C# program.
ClassWhere to use class snippet: press tab tab
class Program
{
static void Main()
{
}
}
class
Output
class Program
{
static void Main()
{
}
}
class MyClass
{
}
Info: After typing cw and then TAB TAB, the caret will be positioned inside the parentheses, so you can immediately start specifying the arguments.
Where to use cw snippet: press tab tab
class Program
{
static void Main()
{
cw
}
}
Output
class Program
{
static void Main()
{
System.Console.WriteLine();
}
}
Note: Of all the loop constructs in the C# language, the do-loop has the shortest initial keyword.
However: If the entire loop structure is too verbose for you, the do snippet can come in handy.
Where to use do snippet: press tab tab
class Program
{
static void Main()
{
do
}
}
Output
class Program
{
static void Main()
{
do
{
} while (true);
}
}
Tip: You must replace the object to be locked upon with the best object. Here, we use the _locker object.
Where to type the lock snippet: C#
class Program
{
static object _locker = new object(); // Lock on this object!
static void Main()
{
lock
}
}
Resulting program: C#
class Program
{
static object _locker = new object(); // Lock on this object!
static void Main()
{
lock (_locker)
{
}
}
}
And: After typing "mbox", press tab twice. You can see in the second part of the example that a call to MessageBox.Show was generated.
Tip: If you try to compile the program in a console program, you will get a compile-time error.
Add reference: Right click on References and go to Add Reference. Click on ".NET" and then click OK on System.Windows.Forms in the dialog.
Where to use mbox snippet: C#
class Program
{
static void Main()
{
mbox
}
}
Result of the snippet
class Program
{
static void Main()
{
System.Windows.Forms.MessageBox.Show("Test");
}
}
Note: You can change this to any type you want. It is a good idea also to change the name from MyProperty to something more descriptive.
PropertyTip: The "prop" snippet in Visual Studio is useful for creating automatically implemented properties.
Where to use prop snippet: press tab tab
class Program
{
prop
static void Main()
{
}
}
Output
class Program
{
public int MyProperty { get; set; }
static void Main()
{
}
}
Tip: You can change this to another type of your choosing. When you press tab after making the change, the type will be matched in the property itself.
Info: The "prop" snippet provides an automatically generated property. And the "propfull" snippet inserts the code for the more verbose syntax form.
Where to type propfull: press tab tab
class Program
{
propfull
static void Main()
{
}
}
Result of the snippet
class Program
{
private int myVar;
public int MyProperty
{
get { return myVar; }
set { myVar = value; }
}
static void Main()
{
}
}
Note: As with several other snippets, the sim snippet is not likely to be useful often. The term "sim" abbreviates "static int Main".
Where to type sim snippet: press tab tab
class Program
{
sim
}
Output
class Program
{
static int Main(string[] args)
{
return 0;
}
}
Info: The svm snippet is the same as the sim snippet except it returns no value. Sim returns an integer value.
Where to type svm: press tab tab
class Program
{
svm
}
Output
class Program
{
static void Main(string[] args)
{
}
}
Where to use switch snippet: press tab tab
class Program
{
static void Main(string[] args)
{
switch
}
}
Output
class Program
{
static void Main(string[] args)
{
switch (switch_on)
{
default:
break;
}
}
}
Result with new switch selection statement
class Program
{
static void Main(string[] args)
{
switch (args.Length)
{
default:
break;
}
}
}
Tip: Typically a using statement has a constructor for an object in the resource acquisition part.
Where to put using snippet: press tab tab
class Program
{
static void Main()
{
using
}
}
Output
class Program
{
static void Main()
{
using (resource)
{
}
}
}
And: Most of the time, a clear understanding of code is more important. Bad code is often worse than useless.