How to code quicker with code snippets in Visual Studio 2019
Code Snippets are small blocks of code that are reusable which can be inserted into a code file using a simple short cut key. Various code snippets are provided by Visual Studio for many languages including C#, C, Visual Basic to name a few.
Use of Snippets make coding more faster and easier. Like in C# just typing if-else, for each and many other snippets and a double tab key provides whole block of code.
To view the available code snippets you can go to Code Snippets Manager from the tools menu.
An example: just typing prop and pressing double tab
Expands to public string MyProperty{get; set;}
Furthermore you can even create your own code snippets with just few steps.
The basic steps are provided below:
1. Create a new XML file in Visual Studio and add the XML code as shown below:
<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
<CodeSnippet Format="1.0.0">
<Header>
<Title>Add Class</Title>
<Author>Sateesh</Author>
<Description>Adds boilerplate code for creating a class</Description>
<Shortcut>addclass</Shortcut>
</Header>
<Snippet>
<References>
<Reference>
<Assembly>System.ComponentModel.DataAnnotations.dll</Assembly>
</Reference>
</References>
<Imports>
<Import>
<Namespace>System.ComponentModel.DataAnnotations</Namespace>
</Import>
</Imports>
<Declarations>
<Literal>
<ID>classname</ID>
<ToolTip>Replace with specified name</ToolTip>
<Default>MyClass</Default>
</Literal>
<Object>
<ID>Object</ID>
<Type>System.Object</Type>
<ToolTip>Replace with a object in your application.</ToolTip>
<Default>myObject</Default>
</Object>
</Declarations>
<Code Language="CSharp">
<![CDATA[
public class $classname${
private static $classname$ myObj= new $classname$();
Object con = $Object$;
public int Id
{
get; set;
}
[StringLength(20)]
public string Name
{
get; set;
}
[EmailAddress]
public string Email
{
get; set;
}
}
]]>
</Code>
</Snippet>
</CodeSnippet>
</CodeSnippets>
The title defines the snippet that is Add Class in our case. The code is for the snippet isprovided in code section inside CDATA. The header section defines the details of the snippets.
2. Save the file with .snippet extension to some locationNow go to tools, click on Code Snippets Manager and import the previously saved snippet file
3. Now add the file to one of the locations in Code Snippets Manager, most prefer My Code Snippets
4. Now the snippet is ready to be used. Create a new console application and type add class and double tab and the snippets expands providing the defination for the class as below:
public class MyClass
{
private static MyClass myObj = new MyClass();
Object con = myObject;
public int Id
{
get; set;
}
[StringLength(20)]
public string Name
{
get; set;
}
[EmailAddress]
public string Email
{
get; set;
}
}
In this way we can create our own code snippet and make the coding process easier and faster.
Also Read: Programming Language to Learn in 2019