C
csharper
I know public fields of a class are not recommended:
//version 1: Not recommended
public class Employee {
public string firstName;
//snip
}
And more often than not, we code it the following way:
// version 2:
public class Employee {
public string FirstName {get; set; }
//snip
}
I assume that version 2 is the preferred approach. But in reality, how is version 2 different from version 1? I understand that in version 2, we have the power to control how we can get and set FirstName, but is it true that the public property FirstName in version 2 *as shown above* is equivalentto the public field firstName in version 1?
Thanks.
//version 1: Not recommended
public class Employee {
public string firstName;
//snip
}
And more often than not, we code it the following way:
// version 2:
public class Employee {
public string FirstName {get; set; }
//snip
}
I assume that version 2 is the preferred approach. But in reality, how is version 2 different from version 1? I understand that in version 2, we have the power to control how we can get and set FirstName, but is it true that the public property FirstName in version 2 *as shown above* is equivalentto the public field firstName in version 1?
Thanks.