Home
Map
Snippet ExamplesUse snippets in Visual Studio, choosing between many snippets for common code constructs.
C#
This page was last reviewed on Apr 13, 2022.
Snippets. This is a useful feature in the Visual Studio IDE. With snippets, you can type a few letters and then press tab twice to insert a code fragment.
Snippet benefits. They help reduce repetitive statements and can reduce overuse injuries from excessive typing. C# code has a lot of repeated syntax.
Ctor. To use ctor, place the cursor inside a class body. Then, type the 4 letters "ctor" and press tab twice. This article shows what happens after you press TAB TAB.
Tip The snippet inserts a parameterless public constructor, which is probably the most common type.
Constructor
However 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.
class Example { ctor } class Program { static void Main() { } }
class Example { public Example() { } } class Program { static void Main() { } }
Class. With the class snippet you insert a new class. You must type the keyword "class" and press tab and then tab again to insert the class definition. The default name will be MyClass.
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.
class
class Program { static void Main() { } } class
class Program { static void Main() { } } class MyClass { }
Cw. In a place where you want to insert a call to Console.WriteLine(), type cw and press TAB twice. The letters "cw" will be expanded to "Console.WriteLine()".
Console.WriteLine
Info After typing cw and then TAB TAB, the caret will be positioned inside the parentheses—you can start specifying the arguments.
class Program { static void Main() { cw } }
class Program { static void Main() { System.Console.WriteLine(); } }
Do. To use the do snippet, type the word "do" and then press tab twice. This will insert do-while code into your program at that position. The "true" will be highlighted so you can change it.
Note Of all the loop constructs in the C# language, the do-loop has the shortest initial keyword.
do while
However If the entire loop structure is too verbose for you, the do snippet can come in handy.
class Program { static void Main() { do } }
class Program { static void Main() { do { } while (true); } }
Lock. The lock snippet can be useful in programs that use threads. This program has a static object added to it that we can use in the lock statement.
Then Type the string "lock" and press the tab key twice. The resulting program will contain the lock statement.
lock
Tip You must replace the object to be locked upon with the best object. Here, we use the _locker object.
class Program { static object _locker = new object(); // Lock on this object! static void Main() { lock } }
class Program { static object _locker = new object(); // Lock on this object! static void Main() { lock (_locker) { } } }
Mbox. With mbox you can insert a simple method call with only a few keystrokes. Type the letters "mbox" into your program. You can use MessageBox.Show in any .NET program.
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.
Detail Right click on References and go to Add Reference. Click on ".NET" and then click OK on System.Windows.Forms in the dialog.
class Program { static void Main() { mbox } }
class Program { static void Main() { System.Windows.Forms.MessageBox.Show("Test"); } }
Prop. Next we use the prop snippet in Visual Studio. To begin, place the cursor somewhere in the scope of a class, where you want to put your property. Type prop and then press tab twice.
Next Visual Studio will select the "int" keyword. You can change this to any type you want.
Also It is a good idea to change the name from MyProperty to something more descriptive.
Property
Tip The "prop" snippet in Visual Studio is useful for creating automatically implemented properties.
class Program { prop static void Main() { } }
class Program { public int MyProperty { get; set; } static void Main() { } }
Propfull. Here we see the propfull snippet in Visual Studio. To begin, put your cursor on a line in the scope of a class. Type propfull and press tab twice.
Info The "prop" snippet provides an automatically generated property. And "propfull" inserts the code for the more verbose syntax form.
class Program { propfull static void Main() { } }
class Program { private int myVar; public int MyProperty { get { return myVar; } set { myVar = value; } } static void Main() { } }
Sim. The sim snippet refers to static int Main. To get started, find a place where you want to insert the Main method. Type sim and then press tab twice.
And A static int Main method with a string array formal parameter will be inserted.
Main args
Note As with several other snippets, the sim snippet is not likely to be useful often. The term "sim" abbreviates "static int Main".
class Program { sim }
class Program { static int Main(string[] args) { return 0; } }
Svm. Next, we use the svm snippet. First position your cursor in a class and then type svm and press tab twice. The static void Main method then appears.
void
Info The svm snippet is the same as the sim snippet except it returns no value. Sim returns an integer value.
class Program { svm }
class Program { static void Main(string[] args) { } }
Switch. Next, we use the switch snippet. To start, type switch and then press tab twice. The "switch_on" identifier is selected. You need to change this to a valid variable.
case
class Program { static void Main(string[] args) { switch } }
class Program { static void Main(string[] args) { switch (switch_on) { default: break; } } }
class Program { static void Main(string[] args) { switch (args.Length) { default: break; } } }
Using. Here we show how and where to use the using snippet. Simply type "using" and then press tab twice. The term "using" will be expanded into the using-statement.
using
Tip Typically a using statement has a constructor for an object in the resource acquisition part.
class Program { static void Main() { using } }
class Program { static void Main() { using (resource) { } } }
Summary. Snippets are useful if you have to type a huge amount of code. You no longer have to type certain constructs. Many developers do not type enough code to really need snippets.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Apr 13, 2022 (edit link).
Home
Changes
© 2007-2024 Sam Allen.