Table of Contents
1 - What are variables
The first thing you need to understand is what variables are. They are names that point to a value that the compiler takes dynamically. Each variable needs to have a type, which determines its size and the way it's stored in memory.
1.1 - Types of variables
We have many types of variables, in the video I only mention the main ones, since the videos are focused on real-world scenarios, and in reality, we only use a few. Here I'll list a few more.
Type | Description |
int, long, sbyte, byte, short, ushort, uint, ulong, char | Integer Numbers |
Float, double | Floating Point Numbers |
Decimal | Decimal Numbers |
Bool | true or false |
null | null types |
In addition to these types, C# allows enums and reference-type variables, and it's very common to use reference-type variables like classes, interfaces, or delegates in the real world, since the goal is to work as close to real-world scenarios as possible, which helps a lot. We'll see reference types in more detail later.
1.2 - Declaring variables
Declaring a variable is very simple; you just need to specify the variable type and its name.
int ejemploVariable;
But in this example the variable doesn't have any value. If we tried to use it, it would tell us it's null
, so it's almost as if we had nothing, and if we tried to operate with it the compiler would give an error. To avoid this, we must assign it a value.
To do this, we have two options: assign the value after the variable is created, or assign the value at the time of the variable's creation.
int ejemploVariable;ejemploVariable = 5;//Second wayint ejemploVariable = 5;
Both examples create a variable ejemploVariable
with a numeric value of 5. Personally, I prefer the second way. If you use the first, the compiler will recommend the second, since it's easier for humans to read.
We can define more than one variable if we separate them by a comma (,
) and remember to put a semicolon (;
) at the end of each statement.
int ejemploVariable, ejemploVariable2;
1.3 - Implicit variable type
Since the release of C# 3, we can use implicit types to create a variable.
What this means is that we don't have to specify the variable type when declaring it, as it will take the type from the initialization value.
var ejemploVariable = 5;
As you can see, we use the reserved word var
, which gets its type from the value assigned to the variable.
You can do this with primitive types as well as custom reference types. Personally, I recommend using the variable type instead of var because when reading code, or doing a "code review" (git and github course), it's much easier to understand.
1.4 - Declaring constants
Sometimes you only need to assign a value to a variable once, as it won't change during the entire execution of the program. For that, we use what is called a constant. This gives us certain advantages over regular variables.
- We make sure that value never changes; in a small project it might not matter, but in a large one, it can help a lot.
- For the above reason, maintenance or fixing errors is much easier since you don't have to look for places where the value changes.
- The compiler is more efficient, since it doesn't need to worry about the value changing, so it can optimize code knowing the value is always the same.
An example of a constant could be the following. Remember, it's a value that never changes.
public const int numeroMeses = 12;
2 - What are operators
Now that we've seen what variables are, let's move on to another crucial topic: operators. Operators allow us to perform both mathematical and logical operations.
C# has a wide variety of operators, as you'll see below.
2.1 - Arithmetic operators
These are the operators that allow us to perform actions, usually mathematical:
Operator | Description | Example |
+ | Adds or concatenates two operands. | A + B = 10 |
- | Subtracts the second operand from the first. | A - B = 5 |
* | Multiplies both operands. | A * B = 25 |
/ | Divides both operands. | A / B = 1 |
% | Modulo operator, returns the remainder of the operation. | A % B = 0 |
++ | Increments the value by one. | A++ returns 6 |
-- | Decrements the value by one. | A-- returns 4 |
This is a small example. If we change the operator to any of the ones above, we will get the corresponding output.
int operadorA = 5;int operadorB = 5;int resultado = operadorA + operadorB; //Resultado nos devolvera 10 en este caso.
2.2 - Relational operators
Relational operators are those that allow us to make comparisons. Suppose the numbers to compare are A = 5, B = 2
:
Operator | Description | Example |
== | Checks if both operands are equal. | (A == B ) returns false |
> | Checks if the value on the left is greater than the one on the right. If it is, the result is true. | ( A > B ) returns true |
< | Checks if the value on the left is less than the one on the right. If it is, the result is true. | ( A < B ) returns false |
>= | Checks if the value on the left is greater than or equal to the one on the right. If it is, the result is true. | ( A > B ) returns true |
<= | Checks if the value on the left is less than or equal to the one on the right. If it is, the result is true. | ( A < B ) returns false |
!= | Checks if both values are different. If they are, it returns true. | ( A != B ) returns true |
2.3 - Logical operators
These are logical expressions and compare booleans. For the example A = true, B = false
:
Operator | Description | Example |
&& | Logical AND operator; if both booleans are true, the result is true. | ( A && B ) returns false |
|| | Logical OR operator; if either one is true, the result is true. | ( A || B ) returns true |
! | Logical NOT operator; used to negate a logical operation. If a condition is true, the NOT operator switches it to false. | !(A && B ) returns true |
2.4 - Assignment operators
These are the operators we use to assign a value to a variable.
Operator | Description | Example |
= | Assignment operator. Assigns a value from the right side to the left side of the operation. | C = A + B ; Assigns the value of A + B to C |
+= | Arithmetic addition and assignment operator; adds the right value to the left value and assigns it. This can be done with any arithmetic operator. | B += A is equivalent to B = B + A |
If there is any problem you can add a comment bellow or contact me in the website's contact form