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.
-
Which version of .Net are you using? – adrianbanks Apr 27 '10 at 19:24
8 Answers
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; }
}

- 1,816
- 1
- 18
- 27
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.

- 129,300
- 32
- 216
- 265
-
-
1
-
3Not really in this case, as Visual Studio has built in refactoring for promoting fields to properties (right click > Refactor > Encapsulate Field) – Matt Greer Apr 27 '10 at 19:40
-
@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 Apr 27 '10 at 20:19
-
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 Apr 27 '10 at 21:12
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.

- 182,639
- 35
- 285
- 343
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.

- 31,040
- 13
- 70
- 99
Why aren't you doing:
public int SomeProperty { get; set; }
or
public int SomeOtherProperty { get; private set; }
?

- 48,267
- 11
- 78
- 120
from this line:
string mytest;
select the whole line "string mytest;", then VS menu: Edit > Refactor > Encapsulate field ... you get this:
public string Mytest { get => mytest; set => mytest = value; }

- 1,017
- 13
- 13
-
try with keyboard shortcut, select fields with mouse and press "ctrl + r, e" (first ctrl + r, next e) and finally press enter. – Gonen09 Nov 06 '19 at 21:46
we can quickly create c# properties in visual studio using prop shortcut and behalf of visual studio tool we can generate c# properties using a tool called c# property generator..
when class has so many properties in it , when we create a object of that class, we have to take certain pain to assign properties so this tool will reduce your pain to certain extent this will automatically assign object with properties..

- 229
- 1
- 4
- 11
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.

- 25,076
- 4
- 67
- 89