0

Say I have private field in a class. If I tell Visual Studio to encapsulate the field with a property, it outputs lambda expressions for the get and set accessors.

namespace MyNamespace 
{
  public class MyClass
  {  
    private bool isActive;

    //Auto-Generated Property
    public bool IsActive
    {
      get => isActive;
      set => isActive = value;
    }
  }
}

But I would rather have a pair of braces for each accessor.

namespace MyNamespace 
{
  public class MyClass
  {  
    private bool isActive;

    //Auto-Generated Property
    public bool IsActive
    {
      get 
      {
        return isActive;
      }
      set 
      {
        isActive = value;
      }
    }
  }
}

How can I change the behavior? I know the snippets exist here: "C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC#\Snippets\1033\Refactoring" But I can't figure out how to change them to get the behavior I want.

Romano Zumbé
  • 7,893
  • 4
  • 33
  • 55
  • 2
    These are not lambdas. This is just a fancy new syntax for declaring getters and setters of properties. It is good. You should want it. – Mike Nakis Jul 02 '17 at 21:10
  • 2
    Except VS 2017 is version "15". Those are the wrong snippets. – Mike Zboray Jul 02 '17 at 21:17
  • Why have a private field anyway? Let the compiler do the work :) – stuartd Jul 02 '17 at 22:19
  • @MikeNakis But what if I wanted to call a method in my setter after I update the value in my private field - like "NotifyPropertyChanged()"? I don't understand how to do that with the new syntax. –  Jul 02 '17 at 23:59
  • @Mike z That was silly of me. I knew something wasn't adding up. –  Jul 03 '17 at 00:01

1 Answers1

0

This is what I was looking for - the propfull shortcut. It is interesting how if you already have a private field, Visual Studio generates a public property with the different (newer) syntax compared to the case when you are starting from scratch.

Shortcut to create properties in Visual Studio?

I used this code snippet to create my own code snippet where I create a public property with backing field. But with the public property I added a RaisedPropertyChanged() method call because I'm doing MVVM with WPF and I want to use this shortcut to easily create properties on ObservableObjects. My question wasn't clear on what my ultimate goal was.

<?xml version="1.0" encoding="utf-8" ?>
<CodeSnippets  xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
    <CodeSnippet Format="1.0.0">
        <Header>
            <Title>obsprop</Title>
            <Shortcut>obsprop</Shortcut>
            <Description>Code snippet for property and backing field for observable object</Description>
            <Author>Microsoft Corporation</Author>
            <SnippetTypes>
                <SnippetType>Expansion</SnippetType>
            </SnippetTypes>
        </Header>
        <Snippet>
            <Declarations>
                <Literal>
                    <ID>type</ID>
                    <ToolTip>Property type</ToolTip>
                    <Default>string</Default>
                </Literal>
                <Literal>
                    <ID>property</ID>
                    <ToolTip>Property name</ToolTip>
                    <Default>MyProperty</Default>
                </Literal>
                <Literal>
                    <ID>field</ID>
                    <ToolTip>The variable backing this property</ToolTip>
                    <Default>myVar</Default>
                </Literal>
            </Declarations>
            <Code Language="csharp"><![CDATA[private $type$ $field$;
    public $type$ $property$
    {
        get { return $field$;}
        set 
        { 
            $field$ = value;
            RaisePropertyChanged();
        }
    }
    $end$]]>
            </Code>
        </Snippet>
    </CodeSnippet>
</CodeSnippets>