Same code — different day.

As I am talking to less experienced C# developers they often say that they are not aware they can write their
own snippets. What’s more, sometimes they are not aware they can use such thing as code snippets!

So what is a code snippet?

According to Microsoft:

Code snippets are small blocks of reusable code that can be inserted in a code file using a context menu command
or a combination of hotkeys. They typically contain commonly-used code blocks such as try-finally or if-else
blocks, but they can be used to insert entire classes or methods.

That’s kinda nice definition, but what does it mean in real life?

Let’s do this by example.


In here I’ve just typed “ctor” -> pressed TAB button tice -> and the code for the constructor has appeared.

By default, Visual Studio gives us many useful code snippets. The full list of the available is at this link.

But let’s say you are working with Xamarin. You are using MVVMCommands and more other stuff. You are kinda bored of writing the same code twice/or copying it from other classes. Of course, if you have exactly the same code, then instead of writing it many times, you should probably move it to another class. But mostly we use templates. Template for Commands, a template for observable Properties, constructor etc. Why not making a snippet which will help us?

Making your own snippet in Visual for Mac.

In order to create your own snippet you should do the following:
Open Visual Studio -> go to Visual Studio Community -> open Preferences & under Text Editor you can find
Code Snippets.

Click Add in the top right corner. The following pop up should appear:

In here we have several interesting edit boxes:

  • shortcut — this is the key which you will be using later to import your snippet,
  • group & mime — in here you can choose whether your snippet is for C#, Html, Python, CSS, Ryzor,
  • description — just a simple description so you can see what for is this snippet.

Let’s create a new snippet!

The “magic” happens when you try to implement your own snippet.

Let’s say I want to create a snippet for creating the command. I want to create such code:

So let’s go to Add CodeSnippet in the preferences.

The shortcut I want is the mcmd — so I will simply write the shortcut as mcmd.

Of course, it is related to C#, so both group and mime are C#.

The description can be “mvvm command”.

Now, let’s think for a moment.

What will be changing in here
? For sure, it will be the name of the private field, the name of the property
and a name of the method.

In my case, all commands have a suffix Cmd and Methods associated with them have the same name. Sticking with my usage, I would have 2 variables.
Name of the private field, and the name of the property. To tell the snippet “this we would like to be inputted
by the user”, we simply put the text in $ marks. So it will look like this.

In the right menu, you can:

* set the default value of the field (in my case “Command”),

* say that this value is editable,

* set the tooltip — this will be used by the IDE to display a tooltip when having the cursor over,

* and function & identifier — telling the truth I haven’t found in the documentation what they are doing
😔. If you know, then please, let me know.

After we are done, press ok and go test if the snippet is working.

As you can see above, mine is working 😎

Pros of creating and using your own snippets:

* allows you to code quicker. Much quicker.


* you need some time to create the snippet for you,

* you need to clean-up the code after using the snippet. eg. when creating the command from the above, you
have a private field, public property and Method in one part of your class. You might need to separate those
manually — in this case, code snippets won’t help you 😔

You can find plenty of snippets on Github, Xamarin forums, etc. As I was changing computers pretty often, I have
even created the repository when I store (almost) all of them.

Some useful snippets:


This article is focusing on Visual Studio for Mac solution. Creating might be different in the
normal Visual Studio on Windows!

Leave a Reply

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