views:

220

answers:

7

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.

+2  A: 

Why aren't you doing:

public int SomeProperty { get; set; }

or

public int SomeOtherProperty { get; private set; }

?

plinth
+2  A: 

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.

LBushkin
Nothing like free advertising.
JustSmith
ReSharper and CodeRush are worth evangelizing.
Chris Farmer
Not really in this case, as Visual Studio has built in refactoring for promoting fields to properties (right click > Refactor > Encapsulate Field)
Matt Greer
@Matt Greer: VS's refactoring support has come a long way, but it still doesn't provide a class-wide or project-wide ways to refactor code.
LBushkin
VS has much better refactoring, but does not come close to Eclipse, a java tool that I have used and liked a lot. Eclipse is better in scope, offering refactoring where it should go and a lot more refactoring methods, like "push up method" when you realize you should have done something in an ancestor.VS with resharper comes closer to Eclipse, but I would want all that in the base product that is not free, by the way.Since VS 2010 has improved refactoring and testing, I would resist a bit more buying some extra add-in.
mico
+9  A: 

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.

Adam Robinson
+9  A: 

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; }
}
mico
+3  A: 

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.

RandomNoob
A: 

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.

Dan Diplo
A: 

You can check this tool from MSDN

http://visualstudiogallery.msdn.microsoft.com/en-us/c736dedf-bbed-454a-8073-89993ca46902

it has a Property Generator

rafaelsr