0

I have seen this kind of code in C#:

  private int id {get;set;}

but I would only create getter for the field, because if there is get and set for it is the same as public field is the only way is:

public int getId(){return id;}

How to automatically generate only getters in VS2010?

halfer
  • 19,824
  • 17
  • 99
  • 186
Yoda
  • 17,363
  • 67
  • 204
  • 344
  • This is very Similar: [How to create Autoporpertys](http://stackoverflow.com/questions/3871270/short-cut-to-create-properties-in-visual-studio) – Venson Jan 19 '13 at 20:39
  • 1
    That is *not* the same as a public field. For at least two reasons. – millimoose Jan 19 '13 at 20:42

6 Answers6

7

Do you mean how do you implement a readonly property? If so try:

public int Id { get; private set;}
Daniel Kelley
  • 7,579
  • 6
  • 42
  • 50
  • But what if I don't want any setter, any. – Yoda Jan 19 '13 at 20:41
  • 1
    If you are writing a base `class`, you can use the `protected` keyword instead of `private` so that derived classes can also set this property. `internal` can also be used, though the cases are pretty rare. `set` is required for auto-generated properties because you would have no other way to set the value of this property (there's no backing storage as if there was a member field defined). – Erik Jan 19 '13 at 20:41
4

What you have implemented is known as an Automatic Property they look like this:

private string Name { get; set; }

Automatic properties merely syntactical sugar and in reality, provide a succinct, quick way to implement this code:

private string _name;
public string Name 
{ 
    get { return _name; } 
    set { _name = value; } 
}

You could disregard automatic properties, using manual proeprties and simply remove the get. Or use automatic properties and make the property's value read only to external members by marking the get with the private access modifier:

public string Name { get; private set; }

The code you say you would usually use, is never really needed in C# because properties, in reality are just methods in disguise and should be used as a better convention:

public int getId(){return id;} //bad
Caster Troy
  • 2,796
  • 2
  • 25
  • 45
  • 1
    @RobertKilar Mostly a convention but if you want to understand the benefits 'under the hood' read: http://csharpindepth.com/articles/chapter8/propertiesmatter.aspx – Caster Troy Jan 19 '13 at 20:48
  • 1
    "properties are methods in disguise" is a rather unfortunate statement. C# properties are implemented using CLR methods, but it's not a good idea to conflate these two abstraction levels. For instance, from C#, you're not allowed to call the emitted methods directly; they are treated specially when using reflection; and who knows what else. So it's obvious they're not *just* methods. – millimoose Jan 19 '13 at 20:48
1

right click on the field, refactor, encapsulated field

Quinton Bernhardt
  • 4,773
  • 19
  • 28
1

No they are not the same. A property compiles to one or two methods bound to a property. E.g.:

public int Foo { get; private set; }

compiles to the IL code that works like this:

private int _foo;
public int Foo { get_Foo = get, set_Foo = set }

public int get_Foo() { return _foo; }
private void set_Foo(int value) { _foo = value; }

In other words: properties are methods, while fields are not. That's why you can do things like:

public int Foo { get { return 0; } }

which compiles to:

public int Foo { get_Foo = get }

public int get_Foo() { return 0;}

update

Okay now I understand your question... the answer is the last part that shows how a getter-only works as well as what it does :-)

atlaste
  • 30,418
  • 3
  • 57
  • 87
1

This is very similar:

How to create Autoproperties

Just type prop and than write your Type and press Tab to write your name.

halfer
  • 19,824
  • 17
  • 99
  • 186
Venson
  • 1,772
  • 17
  • 37
1

I think you want a property that is publicly available, but can only be set by the class. Is it something like this:

 public class Entity
 {
     public void Entity()
     {
         ID = ...;  // Some unique id
     }

     public int ID { get; private set; }
 }

This allows the class Entity to read and write the ID, but other classes can only read the ID.

Richard Schneider
  • 34,944
  • 9
  • 57
  • 73