Quick create C# properties from variables

Right click on the field declaration, menu Refactor -> Encapsulate field and you go from

int n;

to

int n;

public int N
{
   get { return n; }
   set { n = value; }
}

If you're using C# 3.0 or above (VisualStudio 2008, essentially), you can use auto properties. While this isn't exactly what you're asking for, it should (hopefully) do the trick.

Rather than writing:

private string m_Name;

public string Name
{
    get { return m_Name; }
    set { m_Name = value; }
}

You can just write:

public string Name { get; set; }

This will give you quick, "dumb" (i.e. no retrieval or assignment logic) properties that can go on your class. If you find you need retrieval and assignment logic later, just come back and do the full property declaration syntax and you won't have to change any of the calling code.

The only real difference is that you'll have to use the property to get the value within your class, as the backing variable is generated and compile time and unavailable to your code.


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.