Difference between automatic properties and open field in C # 3.0

I was not able to understand why in C # 3.0 there is a function to automatically use language properties.

What difference does it make when you speak

public string FirstName; 

than

 public string FirstName { get; set; } 
+15
Aug 01 '09 at 16:32
source share
7 answers

Because they are implemented differently as a result of IL code (and machine language). The Automatic property is still displayed as a public getter and setter, while a public field is just one field.

Thus, the implementation of the auto property allows at some later time to change the internal behavior of either getter or setter (for example, adding a validator) without recompiling or re = coding any dependent classes that use it ...

+20
Aug 01 '09 at 16:37
source share

To add to what other people have said by declaring a public field, the field is read and write. declaring a public automatic property, although the property is public, you can still add a modifier to control accessibility at the get / set level.

 public string FirstName { get; private set; } 

A user of your class sees FirstName as public. However, he / she cannot write on it.

+8
Aug 01 '09 at 16:50
source share

Think about what happens if you later want to change each of them to a property with a custom implementation. If this is an automatically implemented property, you simply add a field and change the implementation. Full compatibility with source and binary files.

If this field starts, you get neither the source nor the binary compatibility. You must rebuild everything that refers to it and fix everything that no longer compiles.

In addition, properties have different advantages over fields . My main personal objection to fields is that it provides a solution for implementation in the API.

+5
Aug 01 '09 at 16:39
source share

The difference is that other assemblies compiled with the code that reads the property are compiled against the property.

If you later decide that you need to add code to getter or setter, you can do this without having to recompile each assembly associated with it.

Not so with the fields. If you subsequently change the field to be a property to add this code, other assemblies associated with yours will stop functioning as they are compiled to read the field, not the property.

In addition, a lot of code is written to search for properties, not fields, such as data binding, etc.

+2
Aug 01 '09 at 16:38
source share

due to this use:
public string FirstName { get; private set; }
simple property that 'kosher' according to OO rules

+2
Aug 01 '09 at 16:52
source share

The first is a public field, and the second is a public field.

The main difference is how they are used. For example, WPF can only bind data to properties, not fields.

0
Aug 01 '09 at 16:38
source share

Auto properties are compilers that generate regular properties, they use support fields, such as any regular property, but you do not need to write code for this. Here is a very illustrative example (thanks to Reflector ) of the code generated by the compiler:

 [CompilerGenerated] private string <ContentType>k__BackingField; public string ContentType { [CompilerGenerated] get { return this.<ContentType>k__BackingField; } [CompilerGenerated] set { this.<ContentType>k__BackingField = value; } } 
0
Aug 01 '09 at 16:53
source share



All Articles