In this small article, I will explain the default code snippet feature which helps developers to speed up coding while writing their code in Visual Studio. Many of us are aware but some beginners may not be aware of the code snippet feature of Visual Studio which is a really cool inbuilt feature of Visual Studio. This looks very small but it helps in the long run.
Let's take a few code snippet examples which we are using in day to day activity while writing the code like prop, propfull, class, ctor, cw, for etc. I will explain a few of them in details. I will list down a few snippets which we use as a regular day to day activity.
Class | creates a class declaration |
ctor | creates a constructor for class |
prop | creates an auto-implemented property declaration. |
propfull | creates a property decalration with get and set accessor |
propg | creates a read only auto implement a property with private set accessor |
cw | creates console.writeline() |
do | creates do while loop |
for | creates for loop |
foreach | creates a foreach loop |
svm | creates static void main method declaration |
switch | creates a switch statement |
try | creates try block |
try | try block with finally clause |
while | creates while loop |
Let's try some of the above default code snippets. First, we will start with property code snippet and understand how to use it in the day to day activity.
Property
In general, if we create a property in the class, then we will write it like below.
- public class Employee
- {
- public int EmployeeId { get; set; }
- }
In the above example, we wrote property by typing everthing but how can we achieve this using code snippets?
Just type "prop" and hit Tab key twice to insert 'prop' snippet. You are able to see it in the code's right side after hitting Tab key twice.
Full property
If I want to create full property, then I have to write the below code manually but by using the propfull code snippet, we can speed up our code.
- private string firstName;
-
- public string FirstName
- {
- get { return firstName; }
- set { firstName = value; }
- }
Constructor
Let's create a constructor by writing the code manually.
- public class Employee
- {
- public Employee()
- {
-
- }
- }
But if we use a code snippet to create a constructor, we need to type ctor and hit the tab key twice. Our constructor is ready and there is no need to type the code manually.
Console.WriteLine
Like the above code snippet, if we want to write Console.WriteLine, then we have to use cw code snippet and perform the same step like type cw and hit tab twice and it will create Console.WriteLine() for you.
Now, I am not going to explain each code snippet because for every code snippet we need to follow the same thing. Here, l will give a screenshot of how can we identify which are the code snippets available in Visual Studio. Please look at the below snippet; there, I have marked it with an orange circle and pointed to it with the arrow. Once you see square box while typing it means it is the code snippet.
If we try to use the above default code snippet while writing the code that will really help us to write the code more efficiently. I started following this for the last two years and I am able to see a difference now. I hope you also try this if you haven't yet.
Thanks for reading.
I hope it will help someone.