top of page

Visual Studio Snippets - Create snippets easily & quickly with no mess

My journey into C# has been a long and slow one, but with practice I think I am finally getting the hand of the language. Only taken me about 10 years... But with every update to the .Net Framework there's always a new class and update to an old one - in short you're always learning.

C# as a language is probably one of the most common and well supported languages out there; you obviously have Java (easily transferable to C#), C, Python, Ruby, Javascript etc. If you want a more indepth look at the languages go to a wonderful post here

So back to the journey, the longer I work with Visual Studio the more tips and tricks I pick up throughout my professional career.

With that in mind I was recently exposed to #Snippets and how to create them easily - compared to the #MicrosoftWay. More about the manual way here

The efficient coder in me thinks the #MicrosoftWay is too time consuming and when you need to deliver reliable code you just want to create the code, not format the snippet make sure it works and then import your code.

Snippets are a great way to reuse code that you have created previously. Instead of typing out all your code again or trying to find that solution which contained that code you created... (Surely I am not the only one!?!)

So if I wanted to use something simple like:

console.writeline("This is a manually typed piece of code");

You would just type it right?

OK so how about this file logging helper class:

var errorLogLocation = WebConfigurationManager.AppSettings["ErrorLogLocation"];
var fileLogLocation = WebConfigurationManager.AppSettings["FileLogLocation"];
var fileLogName = WebConfigurationManager.AppSettings["FileName"];
var logTxt = "Log my call to file. "
FileLoggingClass.LogToFile(logTxt, "Log", errorLogLocation, fileLogLocation, fileLogName);

You can see from the above I would have to remember what the syntax was for several settings and the FileLoggingClass. With a snippet approach I can add those lines above and just generate the code I need, this way guaranteeing it works and code stability no matter which project I use.

Here's a quick video showing it in action:

Snippet Designer is a free add on to Visual Studio, we have no affiliation to Matthew Manela (The creator) but it's a great little tool.

If you are familiar with Resharper, this type of snippeting is also possible, and as you would expect with a pay for application is pretty awesome and possibly an easier tool than Snippet Designer. But the principle is the same, video to come.

27 views0 comments


bottom of page