Learning C# Notes - Part VI: Handy Code Operators & Shorthands
/Here is a compilation of abbreviations that are frequently used in Unity C# code. Is good to know them so you can understand other’s code and also so you can make your own more compact.
Basic Arithmetic
They are generally used by float, int and their related types.
You can use usual math operators like:
+ for addition (Can also be used for string concatenation)
- for subtraction
* for multiplication
/ for division
% for reminder
Incrementing & Decrementing
Allow you to quickly modify values one unit at a time, useful for iterators or counters.
++ Increments a variable by one.
Example: number++;— Decrements a variable by one
Example: i—;
Arithmetic Assignments
These are a compact way to change a variable value and anger mathematicians at the same time.
Note that the same syntax is also used for subscribing and unsubscribing to delegates, see all about it here.
“+=” is an addition assignment operator.
“x += y” would be the same as saying “x = x + y”.
“-=” works in exactly the same way for subtractions.
You can also find “*=” for multiplications and “/=” for dvisions.
Logic and Comparison
We use “&&” as an AND statement when we want to check that two or more conditions are true.
We use “||” as an OR statement if we want to do something when either condition is true. Only one needs to be true.
We use “!” as a NOT statement to signify that we want to check the opposite of a condition. In other words, the “!” operator inverts a bool, turning a true into false.
When checking that something is bigger or smaller than our condition, we use “<“ and “>”.
We can also use “<=” to check if something is smaller or equal and “>=” to check if it is bigger or equal.
We use “==” to check if a variable equals another.
Ternary expressions
This is a compact way to write conditional statements. A boolean expression is evaluated and two possible result expressions are chosen.
The basic syntax is: “condition ? consequent : alternative;”
If the condition evaluates to true, the consequent expression is used while the alternative expression would be used in case of the condition evaluating false.
A nice way to remember how ternary operations work is to ask yourself:
Is this condition true ? yes : no
Example: See below how the expression in the middle has the same meaning as the ternary expression at the bottom.
int input = new Random().Next(-5, 5); string classify; //Old fashioned if statement: if (input >= 0) { classify = "nonnegative"; } else { classify = "negative"; } //Ternary Expression shorthand: classify = (input >= 0) ? "nonnegative" : "negative";
Properties
When we want to access a variable from another class, we can make the variable public. That works, but can lead to unintended consequences like a variable being changed from the outside when we don’t want this to happen.
Properties are fields that act as a gatekeeper to variables. Can even add logic within them and we can make them read only or write only, giving us much more control than a simple public variable.
See an example below with the standard syntax and then the shorthand version.
//Long way to create a property but allows us additional logic. public int Health { get { return health; } set { health = value; } } //Shorthand way to create a property public int Health {get; set;}
You can also create a quick read only accessor to any private variable that you already have by using the following lambda expression:
private EventInstance m_fmodInstance; public EventInstance FmodInstance => m_fmodInstance;
Adding variable values to strings for debugging (String interpolation)
So sometimes it is very useful to print some info to the console containing values for debugging.
In order to make this more compact and human readable, we can use the “$” operator so we can add out variables between curly braces “{}” and avoid the need to break up our string. See an example below:
//Long form UnityEngine.Debug.LogWarning("The value" + parameter + "is a local parameter."); //Shorthand form UnityEngine.Debug.LogWarning($"The value is a local parameter.");