Tips and Tricks: C# Code Expansion Snippets

Csharp
Csharp

Tips and Tricks: C# Code Expansion Snippets

Welcome to CodeSpread!

Ever felt like not writing the same code again?
Yes! But what is the solution?
Solution is Code Snippets.”

What is a Code Snippet?

Code snippets are small blocks of reusable code which contains commonly-used code blocks such as property, try-finally or if-else blocks.

We just have to type a few letters and then press tab twice [TAB TAB].
This is one of the useful ways to reduce our efforts of writing repetitive code again.

Do we need any special configuration?

No, as it is a feature available in Visual Studio. There are two kinds of code snippet:

  • Expansion snippets, are added at a specified insertion point and may replace a snippet shortcut,
  • Surround-with snippets are added around a selected block of code.

We will talk about expansion snippets in this article and Surround-with snippets in the next article.

Expansion Snippets:

Try Catch Finally: In C#, the shortcut tryf is used to insert a try-finally block:

class CodeSpread
{
tryf
}

type tryf and press TAB + TAB. The result will be

class CodeSpread
{
try
{

}
finally
{

}
}

Property: In C#, the shortcut prop is used to insert an automatically implemented properties block:

class CodeSpread
{
prop
}

type prop and press TAB + TAB. The result will be

class CodeSpread
{
public int MyProperty { get; set; }
}

Full Property: In C#, the shortcut propfull is used to insert a full property block:

class CodeSpread
{
propfull
}

type propfull and press TAB + TAB. The result will be

class CodeSpread
{
private int myVar;

public int MyProperty
{
get { return myVar; }
set { myVar = value; }
}
}

Using: In C#, the shortcut using is used to insert using-statement block:

class CodeSpread
{
static void Main()
{
using
}

}

type using and press TAB + TAB. The result will be

class CodeSpread
{
static void Main()
{
using (resource)
{

}
}
}

Class: In C#, the shortcut class is used to insert class definition block:

class

type class and press TAB + TAB. The result will be

class CodeSpread
{
}

Constructor: In C#, the shortcut ctor is used to insert parameterless public constructor block:

class CodeSpread
{
ctor
}

type ctor and press TAB + TAB. The result will be

class CodeSpread
{
public CodeSpread ()
{

}
}

do-while loop: In C#, the shortcut Do is used to insert do-while code block:

class CodeSpread
{
static void Main()
{
do
}

}

type do and press TAB + TAB. The result will be

class CodeSpread
{
static void Main()
{
do
{

} while (true);
}
}

Switch: In C#, the shortcut Switch is used to insert switch code block:

class CodeSpread
{
static void Main()
{
switch
}

}

type Switch and press TAB + TAB. The result will be

class CodeSpread
{
static void Main()
{
switch (switch_on)
{
default:
break;
}
}
}

There are many other snippets also like Console.WriteLine()[CW],Lock Block[lock], static Main method [sim,svm] etc. Just memorize them and speed up your code skills without much pressure on your hands and head.

Author: hershey

A passion for knowledge drives me to do programming, A passion for programming drives me to create something different, A passion for creation drives me to spread the knowledge.

Share This Post On

0 Comments

  1. nice superb explaination

    Post a Reply
  2. thanks for the tutorial
    I am having a problem.
    The feature “Create SQL server database” isn’t available , I can’t select it.
    Can you help, please ?

    Post a Reply

Submit a Comment

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

More from CodeSpread:

  • Tips and Tricks: C# Code Surround-with SnippetsTips and Tricks: C# Code Surround-with SnippetsWe have discussed Code Snippets and one of the two types, expansion snippets in our previous article: Tips and Tricks: C# Code Expansion Snippets Now, we will discuss Surround-with snippets. ...
  • RegisterStartupScript, RegisterOnSubmitStatementRegisterStartupScript, RegisterOnSubmitStatementWe have discussed RegisterClientScriptBlock, RegisterClientScriptInclude in our previous article here. There are two more methods to client script dynamically from server side. We will discuss thes...
  • Create a browser in C# WinFormsCreate a browser in C# WinFormsHi Guys, This is Sathish, one of my friend was finding difficult in creating his own web browser, so I thought lets create a simple web browser in win forms of c# and share it with you. We are onl...
  • RegisterClientScriptBlock, RegisterClientScriptIncludeRegisterClientScriptBlock, RegisterClientScriptInclude"Current trends are more inclined towards the client model for a better UI experience where small pieces of client script are integrated into the application." These client scripts can be declarat...
  • Tip : Code Structure C#Tip : Code Structure C#As a developer, I always stress on developing a habit of following the best practices to structure the code. It helps me to easily navigate through the code and also saves a lot of effort in search...
  • C# Code to send a MailC# Code to send a MailWhile working in a project, we always neglect the importance of the code which we have written many times and now end up in copy pasting the code from old projects. Lately, I saw a request in fo...
  • Web-Page responses by PostBack, AutoPostBack, CrossPostBack, CallBack.Web-Page responses by PostBack, AutoPostBack, CrossPostBack, CallBack.It is difficult to find a site on the internet which does not respond to user's action. By user's action, i mean to say, clicking on a button, selecting something from a list, navigation links. The...
  • Time Out: Not a Technical postTime Out: Not a Technical postTaking my time-out from technical world for once and sharing a regular joke in the form of  this comic with you. There is no harm in showing funny side also sometimes. wat say!!