Using Code Snippets In Visual Studio Mac

Introduction

As software developers, it's inevitable that we'll end up writing repetitive, boilerplate code that's necessary for our applications to function.

Let's step into the shoes of a Xamarin.Forms developer and think about the repetitive code we write for our view models...

The most common code we might write for a Xamarin.Forms app is view model properties. View model properties often have a public facing property with a backing field, that triggers a property changed event. It might look a little like this:

private string message;

public string Message

{

get

{

return message;

}

set

{

SetProperty(value, ref message, nameof(Message));

}

}

After writing a few of these properties by hand, you'll probably get an itch to make this workflow a tad less repetitive.

Luckily, Visual Studio Mac has a solution for us: code snippets!

A code snippet is block of templated text that includes named arguments that are substituted when inserted by a developer. If we created a code snippet for our view model property, it'd look something like this:

private $type$ $field$;

public $type$ $property$

{

get

{

return $field$;

}

set

{

SetProperty(value, ref $field$, nameof($property$));

}

}

Each section of code to be replaced is surrounded with a $ symbol and represents an argument the developer can control. In the above code snippet, we have: