C# has a system for getters and setters called Properties that Java doesn’t (I probably don’t need to tell you that Java =/= Javascript, but it seems to be a common mistake). These are a really neat way of controlling variables, because they abstract away from using explicit “getX()” and “setX()” methods, allowing them to be used similarly to a regular variable. In terms of actual execution, it is essentially running a method call each time you use it, but it does make the code cleaner. Let’s take an example. In Java, you would write something like this:
public int getX()
{
return x;
}
public void setX(int newX)
{
if(newX < 0)
x = 0;
else
x = newX;
}
This is a simple getter and setter in Java (and, coincidentally, also valid C#), which constrains an integer to be positive, clamping negative inputs to 0. This allows you to use it in this manner:
setX(getX()+4);
This will increase the value by 4. This is how getters and setters work in Java, and they can be used this way in C#, too. However, C# has a neater way of doing it. This is the same getter and setter written in C# using Properties:
public int X
{
get { return x; }
set
{
if(value < 0)
x = 0;
else
x = value;
}
}
This wraps the getter and setter into one item, allowing you to use the value in a more natural way. Instead of the (frankly rather ugly) code I used with the Java example to add 4 to the variable, instead we can do this:
X += 4;
(Note that the X here is capitalised, because X is the property, which is distinct from x, which is a variable.) This essentially runs the same code as above, but it’s much neater, and easier to understand what’s going on. The value
keyword is used to access the implicit argument of the set method, so you don’t have to worry about explicitly defining it. You can also do things like this:
public int Y
{
get { return y; }
private set { y = value; }
}
Which will allow only the parent class to write to this property, but anyone to read it (which is incredibly useful in a lot of situations). And:
public int Z
{
get { return z; }
}
Which creates a read-only property, so that no-one can write to it. This is most useful if you’re returning the result of a calculation, rather than just a variable (note that the get block can contain more than just a return statement, as it is essentially just a method that returns the type that the property is defined to have, you can execute arbitrary code in there, as long as it obeys the rules of the language, of course).
Finally, there is also the implicit property:
public int A
{
get; set;
}
This implicitly creates a variable for the property to read and write to. In this instance, it would work in exactly the same way as a public variable, but you can use access modifiers etc., as above, to alter how you can access the property. For example:
public int B
{
get; private set;
}
I hope this has given you a bit of an insight into how properties work. It’s worth remembering that, though these look like variables when you use them, they are not variables - they are an abstraction for getters and setters, and so when you access a property, you are calling a method, not accessing a variable. Performance-wise, this makes them less efficient than variables, but more efficient than separate getter and setter methods, as they are easier for the compiler to optimise.