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;
            OnPropChanged(propertyName);
        }

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="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  <CodeSnippet Format="1.0.0">
    <Header>
      <SnippetTypes>
        <SnippetType>Expansion</SnippetType>
      </SnippetTypes>
      <Title>Properties</Title>
      <Author> Jesse Liberty  </Author>
      <Description>Insert properties </Description>
      <HelpUrl>
      </HelpUrl>
      <Shortcut>ppropp</Shortcut>
    </Header>
    <Snippet>
      <Declarations>
        <Literal Editable="true">
          <ID>type</ID>
          <ToolTip>e.g., bool</ToolTip>
          <Default>string</Default>
          <Function>
          </Function>
        </Literal>
        <Literal Editable="true">
          <ID>privateVar</ID>
          <ToolTip>backing variable</ToolTip>
          <Default>_backing</Default>
          <Function>
          </Function>
        </Literal>
        <Literal Editable="true">
          <ID>publicProp</ID>
          <ToolTip>property name</ToolTip>
          <Default>Property</Default>
          <Function>
          </Function>
        </Literal>
      </Declarations>
      <Code Language="csharp" Delimiter="$"><![CDATA[
      private $type$ $privateVar$;
      public $type$ $publicProp$
      {
        get => $privateVar$;
        set => SetValue(ref $privateVar$, value);
      }]]></Code>
    </Snippet>
  </CodeSnippet>
</CodeSnippets>

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.