C# Operators Examples - Variables in Unity
C# Operators Examples - Variables in Unity
Operators are special symbols in C# that perform specific tasks, such as mathematical operations, comparison, and logical operations. Here are some examples of operators in C#:
- Arithmetic operators: These operators perform basic mathematical operations, such as addition (+), subtraction (-), multiplication (*), and division (/). For example:
int x = 5 + 6; int y = 10 - 3; int z = x * y;
- Comparison operators: These operators compare two values and return a boolean value (true or false). Examples include equal to (==), not equal to (!=), greater than (>), and less than (<). For example:
bool isEqual = (x == y); bool isGreater = (x > y);
- Logical operators: These operators perform logical operations on boolean values. Examples include AND (&&), OR (||), and NOT (!). For example:
bool result = (isEqual && isGreater); bool opposite = !result;
- Assignment operators: These operators assign a value to a variable. The most basic assignment operator is the equal sign (=), but there are also compound assignment operators that perform an operation and assign the result to the variable in one step. Examples include +=, -=, *=, and /=. For example:
x = 10; y += 5;
- Conditional operator: This operator is a shorthand way to write an if-else statement. It has the following syntax:
condition ? expression1 : expression2
If the condition is true, the operator returns expression1. If the condition is false, it returns expression2. For example:
int max = (x > y) ? x : y;
This code assigns the value of x to max if x is greater than y, or the value of y to max if x is not greater than y.
These are just a few examples of operators in C#. There are many more, including bitwise operators, type operators, and others.
Post a Comment
image video quote pre code