Code Snippet – By request

Code Snippets can save you from repetitive (boring) tasks. Visual Studio comes with a great many code snippets, perhaps the most often used is ctor which creates an empty constructor.

You can also create your own. Here is one I use a lot. It creates something that looks like this:

private string _backing;
        public string Property
            get => _backing;
            set => SetValue(ref _backing, value);

With the cursor on string. Type (for example) bool and the type of both the backing variable and the property itself change to bool.

Hit tab twice and you advance to _backing. Enter your own name for the backing variable and it is replicated in the body of the property as well. Hit tab twice and you are brought to the property name. Enter that and hit return and you exit the snippet.

In order for my particular snippet to work, you have to have the (very popular) SetValue defined, probably in your view model base. Here is what that looks like:

        public event PropertyChangedEventHandler PropertyChanged;

        public void OnPropertyChanged(string propertyName)
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));

        protected void OnPropChanged([CallerMemberName] string propertyName = null)
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));

        protected void SetValue<T>(ref T backingField,
            T value,
            [CallerMemberName] string propertyName = null)
            if (EqualityComparer<T>.Default.Equals(backingField, value)) return;
            backingField = value;

The point of this code is that SetValue will do the right thing for PropertyChanged events.

Here is the complete snippet. To use this open Tools -> Code Snippets Manager. When the dialog box opens, be sure to use the drop down at the top to switch from Basic to C#. Then click Add and copy and paste this code.

<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets xmlns="">
  <CodeSnippet Format="1.0.0">
      <Author> Jesse Liberty  </Author>
      <Description>Insert properties </Description>
        <Literal Editable="true">
          <ToolTip>e.g., bool</ToolTip>
        <Literal Editable="true">
          <ToolTip>backing variable</ToolTip>
        <Literal Editable="true">
          <ToolTip>property name</ToolTip>
      <Code Language="csharp" Delimiter="$"><![CDATA[
      private $type$ $privateVar$;
      public $type$ $publicProp$
        get => $privateVar$;
        set => SetValue(ref $privateVar$, value);

Returning to the code snippets manager you should now see a nice description of your sparkling new code snippet.

That’s it. Easy peasy.

About Jesse Liberty

Jesse Liberty has three decades of experience writing and delivering software projects and is the author of 2 dozen books and a couple dozen Pluralsight & LinkedIn Learning courses. He was a Senior Technical Evangelist for Microsoft, a Distinguished Software Engineer for AT&T, a VP for Information Services for Citibank and a Software Architect for PBS. He is a Xamarin Certified Mobile Developer and a Xamarin MVP and a Microsoft MVP.
This entry was posted in Essentials and tagged . Bookmark the permalink.