C # auto properties

I am a little confused at the point Automatic properties in C #, for example,

public string Forename{ get; set; } 

I get that you save the code without declaring a private variable, but what is the point of the property when you don't use any get or set logic? Why not just use

 public string Forename; 

I'm not sure what the difference is between these two statements, I always thought that you used properties if you need additional get / set logic?

+52
Aug 18 '09 at 15:32
source share
11 answers

The code can be inserted into the properties without violating the contract, fields cannot be placed in them without changing their properties (and breaking the interface). Properties can only be read or write, fields cannot. Properties can be connected by data, fields cannot.

+116
Aug 18 '09 at 15:34
source share

You can write

 public string Forename{ get; private set; } 

to get read-only properties ... Still not as universal as real properties, but this is a trade-off that works for some.

+15
Aug 18 '09 at 15:36
source share

I'm not sure what the difference is between these two statements, I always thought that you used properties if you need additional get / set logic?

In the first case, the compiler will automatically add a field to you and wrap it. This is basically equivalent to:

 private string forename; public string Forename { get { return this.forename; } set { this.forename = value; } } 

There are many advantages to using properties over fields. Even if you don’t need some specific reasons, such as data binding, it helps the future API.

The main problem is that if you create a field, but in v2 of your application, you need a property, you will break the API. Using the automatic property up, you can change your API at any time without fear of problems with the original or binary compatibility.

+13
Aug 18 '09 at 15:37
source share

This means that you expect to add logic later.

If you do this and acquire it as a property from the start, you don’t have to rebuild the dependent code. If you change it to a variable on a property, then you have to.

+6
Aug 18 '09 at 15:34
source share

Browse some related threads about The difference between automatic properties and public fields , Fields versus properties , Automatic properties - Useful or not? Why not use public fields .

+4
Aug 18 '09 at 15:45
source share

Members of public data are evil (in that the object does not control the modification of its own state - it becomes a global variable). Interrupts encapsulation - the principle of OOP.

The automatic properties are to provide encapsulation and avoid difficulties when writing boiler plate code for simple properties.

 public string ID { get; set;} 

You can change automatic properties to non-automatic properties in the future (for example, you have some checks in the setter, for example) ... and not break existing clients.

 string m_ID; public string ID { get { return m_ID; } set { //validate value conforms to a certain pattern via a regex match m_ID = value; } } 

You cannot do the same with public data attributes. Changing the data attribute for a property will force existing clients to recompile before they can interact again.

+2
Aug 18 '09 at 15:35
source share

First, you can set the property to virtual and implement the logic in the inheriting class. After that, you can also implement the logic in the same class, and there will be no side effects on any code based on the class.

+1
Aug 18 '09 at 15:33
source share

When adding automatic properties, the compiler will add get set logic to the application, which means that if you add this logic later, and links to your property from external libraries will continue to work.

If you switched from a public variable to a property, this will be a change for other libraries that reference you - hence, why not start with the auto property? :)

+1
Aug 18 '09 at 15:39
source share

Not all properties need to get / set logic. If so, you are using a private variable. For example, in the MV-something template, your model will not have much logic. But you can mix and match as needed.

If you used a field, as you suggested instead of a property, you cannot, for example, define an interface to correctly describe your class, because interfaces cannot contain data fields.

+1
Aug 18 '09 at 16:05
source share

The property is like a contract, and you can change the implementation of the property without affecting the clients using your classes and properties. Today you may not have any logic, but as business requirements change, and if you want to enter some code, then properties are your safest bet. The following 2 links are great C # videos. The first explains the need for properties only when using fields, and the second video explains the different types of properties. I found them very useful.

The need for properties in C #

Ports in C #, read only, write only, read / write, automatic execution

+1
Jun 26 2018-12-12T00:
source share

Take a look at the following code and explanation.
The most common implementation for a property is getter or a setter that simply reads and writes to a private field of the same type as a property. An automatic property declaration instructs the compiler to provide this implementation. The compiler automatically generates a private backing field.
Look at the following code: -

  public class Stock { decimal currentPrice ; // private backing field. public decimal CurrentPrice { get { return currentPrice ; } set { currentPrice = value ; } } } 

The same code can be rewritten as: -

  public class Stock { public decimal CurrentPrice { get ; set ; } // The compiler will auto generate a backing field. } 

SOURCE: - C # in a nutshell

0
Dec 10 '13 at 12:21
source share



All Articles