C# Getters and Setters Help!
A property is a member that provides a flexible mechanism to read, write, or compute the value of a private field. Properties can be used as if they are public data members, but they are actually special methods called accessors. This enables data to be accessed easily and still helps promote the safety and flexibility of methods.
Here's a somewhat useful example . In this example, the TimePeriod class stores a time period. Internally the class stores the time in seconds, but a property named Hours enables a client to specify a time in hours. The accessors for the Hours property perform the conversion between hours and seconds.
class TimePeriod
{
private double seconds;
public double Hours
{
get { return seconds / 3600; }
set { seconds = value * 3600; }
}
}
class Program
{
static void Main()
{
TimePeriod t = new TimePeriod();
// Assigning the Hours property causes the 'set' accessor to be called.
t.Hours = 24;
// Evaluating the Hours property causes the 'get' accessor to be called.
System.Console.WriteLine("Time in hours: " + t.Hours);
}
}
// Output: Time in hours: 24
As far as the "get" and "set" things go...they are called accessors. The accessor of a property contains the executable statements associated with getting (reading or computing) or setting (writing) the property. The accessor declarations can contain a get accessor, a set accessor, or both. The declarations take the following forms:
set {accessor-body}
get {accessor-body}
But I've already shown you an example of what exactly they do , so I won't go more in-depth here.
But what's the difference? Well...let's say we had this :
private string _name;
public string Name { get { return _name; } set { _name = value; } }
We could also do something like this :
public string get_Name() { return _name; }
public void set_Name(string value) { _name = value; }
Both of them act identically. And in fact, this is exactly what the compiler does for you when you create a property.
So it really comes down to proprieties. Should you use a property over a method? Honestly , it doesn't matter too much. In general, methods represent actions and properties represent data. Properties are meant to be used like fields, meaning that properties should not be computationally complex or produce side effects. Symantically properties are attributes of your objects. Methods are behaviors of your object Consider a class , Car { Color, Model, Brand } . A car has Color, Model and Brand attributes therefore it does not make sense to have a method SetColor or SetModel because symantically we do not ask Car to set its own color. When it does not violate the following guidelines, some might prefer using a property, rather than a method, because less experienced developers find properties easier to use. If you're doing something complex that may affect several data members, a method is more appropriate. Or if your getter takes parameters or your setter takes more than a value parameter.
So , I'll just say it again , it doesn't really matter. Functionality is identical. Just make sure you stick to one style.

Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now