Hello,
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.
Hello,
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.
Why aren't you doing:
public int SomeProperty { get; set; }
or
public int SomeOtherProperty { get; private set; }
?
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.
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.
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; }
}
FYI, simply typing "prop" (no quotes) triggers one of the snippets that comes with VS, and you just tab your way through, by far the quickest option.
You probably should be using Auto-Implemented properties in C# for most things. However, if you want 'old-style' properties with explicit backing fields you can create a Visual Studio code snippet to make them easier to write. This blog post has an example of one.
You can check this tool from MSDN
http://visualstudiogallery.msdn.microsoft.com/en-us/c736dedf-bbed-454a-8073-89993ca46902
it has a Property Generator