.NET Tips and Tricks

Blog archive

Visual Studio Tip: Write a Property with Just a Name and a DataType

Sometimes auto-implemented properties won't do the job, and you need a full property declaration; for instance, if you want to include a call to NotifyPropertyChanged in your property's setter or if you want a read-only property in Visual Basic. However, just because you need a full property, you don't have to type all the required code in.

In C#, just type in the definition for the property's backing field. A property called LastName might have this as its backing field:

string lastName;

To create the property from the field, just right-mouse click on the declaration and, from the Refactor menu, select Encapsulate Field. Visual Studio will pop up a dialog box that will let you name the property and set some options on it (and you may not even have to do that much -- for my example, the dialog will default the property name to LastName, with an initial uppercase letter).

In Visual Basic, just type "pro" and press the Tab key twice (once to complete the keyword "Property" and the second time to trigger inserting the Property code snippet). Visual Studio will write your property with a backing field, a setter, and a getter. Type in your backing field's name and, when you press the Tab key again, you'll be moved to the backing field's data type. After setting the data type, press the Tab key one last time to move to the Property name and enter it. Everything else is taken care of for you.

Not all code snippets are as obvious as the property snippet. If you want Visual Studio to write the skeletons of your constructors, for instance, type ct and press the Tab key twice. Visual Studio will write a default constructor in your project's language and then position your cursor inside the constructor. Since I keep switching between C# and Visual Basic, I find this snippet especially useful: I don't have to pay attention to the differences in the languages.

Posted by Peter Vogel on 06/11/2013 at 1:16 PM

comments powered by Disqus


  • Microsoft's Lander on Blazor Desktop: 'I Don't See a Grand Unified App Model in the Future'

    For all of the talk of unifying the disparate ecosystem of Microsoft-centric developer tooling -- using one framework for apps of all types on all platforms -- Blazor Desktop is not the answer. There isn't one.

  • Firm Automates Legacy Web Forms-to-ASP.NET Core Conversions

    Migration technology uses the Angular web framework and Progress Kendo UI user interface elements to convert ASP.NET Web Forms client code to HTML and CSS, with application business logic converted automatically to ASP.NET Core.

  • New TypeScript 4.2 Tweaks Include Project Explainer

    Microsoft shipped TypeScript 4.2 -- the regular quarterly update to the open source programming language that improves JavaScript with static types -- with a host of tweaks including a way to explain why files are included in a project.

  • What's Top-Paying .NET Skill, In-Demand Language?

    New tech reports reveal the top-paying .NET skills and most in-demand programming languages in the Microsoft-centric developer landscape.

Upcoming Events