Article image Variables, constants and operators in C#

13. Variables, constants and operators in C#

Page 13 | Listen in audio

Before we dive into game programming with Unity, we need to understand the basic concepts of the C# programming language, which is the main language used in Unity. In this chapter, we will cover three fundamental concepts: variables, constants and operators.

Variables

In C#, a variable is a named storage location that our program can manipulate. Each variable in C# has a specific type, which determines the size and memory layout of the variable, the range of values ​​that the variable can store, and the set of operations that can be applied to it. For example, a variable of type 'int' in C# can store an integer, while a variable of type 'string' can store a sequence of characters.

To declare a variable in C#, we use the following syntax: 'type variable_name;'. For example, to declare an integer variable called 'score', we would write 'int score;'. We can then assign a value to this variable using the '=' assignment operator, as in 'score = 10;'.

Constants

A constant in C#, as the name suggests, is a type of variable whose value cannot be changed. Once a value is assigned to a constant, it cannot be changed later. Constants are useful when you have values ​​that are used frequently in your program and that do not change.

To declare a constant in C#, we use the following syntax: 'const type constant_name = value;'. For example, to declare an integer constant called 'maxScore' with a value of 100, we would write 'const int maxScore = 100;'. Note that the value of a constant must be provided at the time of declaration.

Operators

Operators in C# are special symbols that perform specific operations on one or more variables and return a result. C# includes a wide variety of operators, which can be categorized as arithmetic operators, comparison operators, logical operators, assignment operators, and so on.

Arithmetic operators are used to perform basic mathematical operations. For example, the '+' operator is used for addition, the '-' operator is used for subtraction, the '*' operator is used for multiplication, and the '/' operator is used for division. Additionally, the '%' operator is used to obtain the remainder of a division, and the '++' and '--' operators are used to increment and decrement a value respectively.

Comparison operators are used to compare two values. For example, '==' operator is used to check equality, '!=' operator is used to check inequality, '>' and '<' operators are used to check whether a value is greater or less than another, and the '>=' and '<=' operators are used to check whether one value is greater than or equal to, or less than or equal to, another, respectively.

Logical operators are used to combine two or more conditions. For example, the '&&' operator is used for the logical AND operation, the '||' operator is used for the logical OR operation, and the '!' is used for logical NOT operation.

Finally, assignment operators are used to assign a value to a variable. The '=' operator is the basic assignment operator, which simply assigns the value on the right to the operand on the left. Additionally, C# provides compound assignment operators, such as '+=', '-=', '*=', '/=', and '%=', which perform an arithmetic operation on the left operand and then assign the result to the left operand.

In summary, variables, constants, and operators are the fundamental building blocks of C# programming. Understanding how to use these concepts is essential for programming games with Unity. In the next chapter, we'll explore how to use these concepts to create game scripts in Unity.

Now answer the exercise about the content:

What is the main difference between a variable and a constant in C#?

You are right! Congratulations, now go to the next page

You missed! Try again.

Article image Control structures in C#

Next page of the Free Ebook:

14Control structures in C#

4 minutes

Earn your Certificate for this Course for Free! by downloading the Cursa app and reading the ebook there. Available on Google Play or App Store!

Get it on Google Play Get it on App Store

+ 6.5 million
students

Free and Valid
Certificate with QR Code

48 thousand free
exercises

4.8/5 rating in
app stores

Free courses in
video, audio and text