.NET Auto-implemented properties

Also known as automatic properties.

From the start we have classic properties:


Private _Name As String
Public Property Name As String
        Return _Name
    End Get
    Set(ByVal value As String)
        _Name = value
    End Set
End Property

A shortcut (snippet) for this in Visual Studio is type property then hit tab


private String _Name;
public String Name
        return _Name;
        _Name = value;

A shortcut (snippet) for this in Visual Studio is type propfull then hit tab

Then C# 3 (2008) came out with auto-implemented properties:

public String Name { get; set; }

A shortcut (snippet) for this in Visual Studio is type prop then hit tab

Later VB.NET 10 (2010) caught up:

Public Property Name As String

Though I don’t know of a built in snippet, it is easy enough to create your own or use property and remove the extra code.

Of course these shortcuts might change, but work for me šŸ™‚


3 responses to “.NET Auto-implemented properties

  1. Just one of the things (or properties at all!) that makes C# much nicer than Java to code in, shame Java isn’t keeping up.

    • I’m gradually moving older entries from my personal wiki over to wordpress, hence these sometimes trivial posts with old dates šŸ™‚

    • Is Java wrong on this, I’m undecided about properties, are they required?

      A read only property is just a getter for a (possibly) private field.
      A write only property is just a setter etc.
      Sure they are cleaner, but are they any different than a method invocation? Performance difference? I expect is all just semantics.

      Auto-implemented properties? What’s the point in them?
      The only thing I can think of is you can later change to a more complex implementation within the property without having to recompile anything that uses this, whereas the switch between public field to public property with the same name would require recompilation… been there before!

      All this until…. object intializers, that’s really where (auto-implemented) properties win my attention…
      Person me = new Person(“Tim”) { Middlename = “The”, Surname = “Enchanter” };

      So you have constructors who’s job it is to leave the new instance in a valid state and the properties can be used to set additional optional values.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s