Top 12 tricks to insert common code snippets : Productivity Tips

This post is in line with my previous post where I talked about how one can be more productive with Visual Studio. In last post, We discussed top 12 shortcuts that helps a lot while writing and reviewing code in day to day work. While writing code we need to follow to follow certain rules and constructs that also becomes very repetitive say, you are writing classes for your domain or adding properties or defining constructors or putting try block and lot more where we just need to write the code in similar pattern and are no brainer. Spending time in these types of code are kind of waste of time and energy. To see my previous post, use the link below

Top 12 Visual Studio short cuts – Productivity Tips

In this post, I am going discuss 12 tricks that can insert code snippets just by few keystrokes.

1- Writing a class is a one of the most common repetitive code we write. Use

  class press TAB





Once the snippet inserted, it puts the cursor at MyClass which allows to change the name of class instantly without any extra keystroke.

2- Writing constructor is also another task which is very common and many times we many constructor for the same class. To write constructor use

ctor press TAB





3- Now Class is created. Let’s add property now. Use

prop press TAB




Similar as 1st trick, it puts the cursor to change the type and after that change the property name itself. I found really cool when I used first.

It comes with many other flavor. Use propg  to create a auto implemented property with private set.

4- Second trick allows us to write auto-implemented property which requires less code but there could be many scenarios, when we need to write Full property definition that we used in earlier to C# 3.0 then use

propfull press TAB






Changing type and name can be done similar to above.

5- Need to write an indexer, use

indexer press TAB





6- Many times while code review we see that some part of code is not under try catch block or we need to write the block. Use

try press TAB






There is another flavor, if we just want try and finally then use tryf.

7- Want to create enum use

enum press TAB





Similarly use struct to insert struct block.

8- Want to create switch block, use

switch press TAB





Similarly use for, foreach, do, while etc to insert respective code blocks.

9- Many times we need to write our own custom exception class that should inherit from Exception class. It can written easily by

Exception press TAB

It creates a serializable class as

8- Exception







10- Want to override Equals method for your class. Use

equals press TAB

It will insert as












11- To write static void main, use

svm press TAB





Similarly you can use sim for static int main.

12 – last, many times we write console.writeline in our application. Just use

cw press TAB



In this post, how can we save lots of keystrokes and time while writing some very common repetive code. This is not complete list but very common ones that can be used a lot in our day to day coding. if you know any other snippet which is very useful, do share in comments.



Exploring Enums support in ASP.NET MVC 5.1 – Part 2

This is second part of the series of exploring enums support in ASP.NET MVC 5.1 that was recently got released with Visual Studio 2013 Update 2. The first part of the series can be read from the below link

Enums support in ASP.NET MVC 5.1- Part 1

In last post we discussed that how the new scaffolding works fantastic for enum types as well. But there are other different scenarios where we need to customize it before displaying on UI. We discuss the issue in one of the sceanio in last post. To handle these scenarios, few other new helper methods got introduced. A new class EnumHelper got added which has two methods :

GetSelectList – which has four overridden method and it returns a list of SelectListItem which can be used to further populate the dropdown in UI.

IsValidForEnumHelper– This method also has two overridden method that tells whether the passed type is enum. This should be used to check the type of passed value and GetSelectList should be used if IsValidForEnumHelper returns true.

So let’s jump to the example and use the above methods.

In part-1 of the series, we used the following enum in the example

public enum ExpertiseArea

but as we know that enum properties can not include space or some special characters. Say, if we want to display WindowsAzure as Windows Azure or ASPNET as ASP.NET etc. In this scenario, we need to create custom views for edit and display purposes.

First let’s change the enum itself and add the display attributes as

    public enum ExpertiseArea
        [Display(Name = "ASP.NET")]
        [Display(Name = "C#")]
        [Display(Name = "Windows Azure")]

Now let’s create the partial view for display scenario.  Here we need to cater multiple scenarios like

– If there are some items which has some selected value for enum property then find the selected text/vale. SelectedText represent the display attribute

– If nothing is selected then show the empty value. (Empty value cannot be converted in enum type)

– If the type itself is not an enum then use the normal value.

We can write it as

@model Enum

@if (EnumHelper.IsValidForEnumHelper(ViewData.ModelMetadata))
    string displayAttrName = null;
    foreach (SelectListItem item in EnumHelper.GetSelectList(ViewData.ModelMetadata, (Enum)Model))
        if (item.Selected)
            displayAttrName = item.Text ?? item.Value;

    // displayAttrName is not assigned to any value
    if (String.IsNullOrEmpty(displayAttrName))
        if (Model == null)
            displayAttrName = String.Empty;
            displayAttrName = Model.ToString();

    @Html.DisplayTextFor(model => displayAttrName)
    @Html.DisplayTextFor(model => model)

Make sure that above Partial View enum.cshtml is created in EditorTemplates folder under the Shared directory.

So if we see the above code, then we find that EnumHelper.IsValidForEnumHelper is used to check that whether the passed value is of enum type and the other one EnumHelper.GetSelectList which returns the list of items in SelectListItem type as discussed.

Now let’s us create the partial view for Edit purposes as

@model Enum

@if (EnumHelper.IsValidForEnumHelper(ViewData.ModelMetadata))
    @Html.EnumDropDownListFor(model => model, htmlAttributes: new { @class = "form-control" })
    @Html.TextBoxFor(model => model, htmlAttributes: new { @class = "form-control" })

This partial view also should be in EditorTemplates folder under the Shared directory. Here again the new helper method EnumHelper.IsValidForEnumHelper which tells whether the provided model data is of type enum or not. When it is of type enum then it uses @Html.EnumDropDownListFor else uses normal @Html.TextBoxFor

Now let’s see the Create/Edit views and here we have not used
@Html.EnumDropDownListFor instead used normal @Html.EditorFor which uses the display and edit templates that we created

   @Html.EditorFor(model => model.Area)

Now let’s run the application, in create page looks like


and after adding Index look as

part2-2Here the earlier issue is resolved and we are able to see the consistent values on diffrent CRUD screen. We can write the similar custom code in other scenarios as well.

Happy learning,