user204588 user204588 - 5 months ago 16
C# Question

Quick create C# properties from variables

For C#, I hate writing out the variables and then writing out all the properties. Isn't there a way to select all variables, right click and create all the properties.

Answer Source

Are you looking for a code refactoring tool? If so, check out ReSharper. It provides an easy to to turn simple field-backed properties into auto-properties, and vice versa.

If you simply don't want to write custom field-backed properties, you can use auto-properties, fpor example, like so:

public string MyProperty { get; set; } // generates an auto-property

which is equivalent to:

private string m_MyProperty;
public string MyProperty 
  get { return m_MyProperty; }
  set { m_MyProperty = value; }

You can even make the accessibilty of the setter and getter difference:

public string MyProperty { get; private set; }

If you do choose to use auto-properties, be aware that you cannot access the underlying field, nor can you supply an implementation for just one portion (just the getter or just the setter). You can, however, make the property virtual.