Difference between Automatic Properties and public field in C# 3.0

I failed to understand why auto implemented property language feature exist in C# 3.0.

What the difference it is making when you say

public string FirstName;

than

public string FirstName { get; set; }

Solution 1:

Because they are implemented differently in the resulting IL code (and machine language). An Automatic property is still exposed as a public getter and setter, whereas a public field is just that - a single field..

Thus, implementing an auto property allows you at some later date to change the internal behavior of either the getter or setter (like adding a validator) without recompiling or re=coding any dependant classes that use it...

Solution 2:

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

public string FirstName { get; private set; }

The user of your class sees FirstName as public property. However, he/she cannot write to it.

Solution 3:

Consider what happens if you later want to change each of them to a property with a custom implementation. If it's an automatically implemented property, you just add a field and change the implementation. Full source and binary compatibility.

If it's a field to start with, you get neither source nor binary compatibility. You have to rebuild everything that references it, and fix up anything which no longer compiles.

Additionally, properties have various benefits over fields. My main personal objection to fields is that it exposes an implementation decision in the API.