Working with Property in C#

Suleiman Abdullah
4 min readSep 5, 2024

--

Objective: Working with Property in C#.

Before creating property create two class Enemy and GameManager class.

To create a property first we need to create a normal variable, go to game Manager and create a variable called _isGameOver.

Now we need to create a property, property use public modifier follow data type followed by the name of the property. Then open and close curly brackets. The type must be equal to the variable type.

In order to access our property inside the curly bracket we use get key word open and close curly bracket again, then we use key words to return the our normal variable.

A get is called access accessor, this will enable other class to access _isGameOver status but not set its value.

To enable other class to set the value of the property we need to add other Accessor using keyword set, then we assign the normal variable with keyword value.

Setting our property to private set.

This means no other class can set the value only the base class(GameManager/parent class) can set the value.

We add private modifier before the accessor type in set accessor.

Note I will remove private modifier in a set accessor.

To test this go to the enemy class create this variable.

For testing I will set this variable for the sake of this article, then i will add simple logic to check if enemy count is zero plus I will create game manager object to access the property.

Note: Its up to you how you access it whether by get component or other way.

Now to set game over i will call our property and set its value to _gameState.

Normal property can run logic, inside of it.

Normal property can have get accessor only.

We can have get accessor only and set the value inside the GameManager.

Auto Property

An auto property does not need normal variable.

To define auto property we use public key word follow with type open and close curly bracket.

Inside the curly bracket we add get or set keyword follow by semicolon

Note: Auto property can not run a logic/code inside it.

Also we can set auto property to private set only, means only this class can set the value.

Auto property must have both accessor, in order to set value even inside its class you must add private set .

Note: We cant set the value from the other class since its private set.

Best Practice When using Property.

  1. When using normal property its best practice to use camel case to our variable name and Pascal case to our property name.
  2. Its best practice to use the same name of property as variable name when using normal property.
  3. Its best practice to set the value to private or protected so only a class need to set the value able to set. Go back to my article to know how to use protected this means only a class Inherit this class can change value.

This is how to use property See in the next article.

--

--

Suleiman Abdullah

Self taught Unity Developer, who is passion about making games ,and he loves Math For Game Development