Is there some internal difference between the C# syntactic sugar way of making properties:
public string FirstName { get; set; }
and just making public variables like this:
public string LastName;
I assume the first way is preferred and the second to be avoided. However, I often see this type of readonly property being used which is a form of the second type above:
public readonly string InternalCode;
Is this a best-practice way to create readonly property?
using System; namespace TestProps { class Program { static void Main(string[] args) { Customer customer = new Customer(); customer.FirstName = 'Jim'; customer.LastName = 'Smith'; customer.Show(); } } class Customer { public string FirstName { get; set; } //prefered public string LastName; //avoid public readonly string InternalCode; //??? public Customer() { InternalCode = '234729834723984'; } public void Show() { Console.WriteLine('{0}, {1} ({2})', LastName, FirstName, InternalCode); Console.ReadLine(); } } }
Since he didn’t answer (yet) and no one else referenced this yet: There is a great article on this topic by Jon Skeet amending his book C# in depth (give credits to Jon):