It has been a while since I last posted, it has been a busy year with a new javascript development job and my daughter being born. Things have started to settle down a little, so I thought it might be a good time to kick things off by diving into some c# tutorials, I will then likely return to some Unity specific stuff, and maybe even javascript game dev posts.

I have had a few questions from people asking me to elaborate on the concept of Getters and Setters in C#, specifically around the { get; set; } syntax (also known as an auto property).

If you have had any dealings with C# you will likely have seen variables declared in the following format:


public class MyClass
{
    public string myProperty { get; set; }
}

The above implementation is known in CSharp as an Auto Property, or Auto Implementation Property, because when the code is compiled it will automatically convert the line into a more traditional getter / setter function block such as this:

public class MyClass
{
	private string _myProperty ; //also known as a 'backing field'
	public string MyProperty
	{
		get
		{
			return myProperty ;
		}
		set
		{
			myProperty = value ;
		}
	}
}

Basically speaking, it’s a shorthand single line version of the more verbose implementation directly above.
They are typically used when no extra logic is required when getting or setting the value of a variable.

The values can then be accessed and mutated (changed) from an external class like so:

myClass = new MyClass()
string foo = myClass.MyProperty; //getting
myClass.MyProperty = "john"; //setting

You can define any combination of get or set depending on how you would like your property to be accessible, for Instance:

public string myProperty { get; }

This will allow an external class to get the value of this property, but not set it – a useful way to protect against data mutation.

When new to C#, it may be tempting to just give all properties both getters and setters, which unless they explicitly need to be mutable, is bad practice.

“Tell an object what to do, don’t ask it for information and manipulate it yourself”. With that in mind, don’t add setters and getters by default. If a variable does not need to be accessed at all by other classes, make it private instead.
And if there is need for external class access, try to have a good reason to use a setter!

Discussions on the ethics of data mutation / immutability can get complex fast depending on who you are speaking to, but if you are a beginner, do not fret too much, as the more elaborate concepts will present themselves as your journey continues. Master the basics first.

Let’s Summarise

Avoid making all properties public, because it's just a free for all and how spaghetti code is cooked up.

public string myValue; // don't do this

If you have a property that does NOT need to be accessed by other classes, make it private.
And If coming from Unity, and you want to expose the variable in Unity’s property inspector, serialize it instead like so:


[SerializeField] //expose the variable in the property inspector, even though it's private
private string _myValue;

If you require access to a class member variable’s value from another class, give it a getter like so:


public string myValue { get; }

And if you should also need to change the data from another class, you can add a setter also:


public string myValue { get; set; }

As mentioned earlier, be mindful of using setters, as it basically means anything can change anything else, which can lead to all types of problems once a project grows.