Index
1 - What are variables
The first thing we should consider is what variables are. They are names that point to a value which the compiler takes dynamically. Each variable needs to have a type which determines its size and the way it is stored in memory.
1.1 - Types of variables
There are many types of variables. In the video I only list the main ones, as the videos are focused on real-world usage, and in practice we only use a few types. 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, C# allows for enum and reference type variables, and it's very common to use reference types like classes, interfaces, or delegates in the real world, since working in that way makes our code more aligned with reality. We'll cover reference types later.
1.2 - Variable declaration
Declaring a variable is very simple, we just need to specify the type and its name.
int ejemploVariable;
But in this example, the variable doesn't have a value. If we tried to use it, it would tell us it's null
, so it's as if we had nothing, and if we try to operate with it, the compiler will throw an error. To avoid this, we should assign a value.
To do this, we have two options: assign a value once it's created, or assign the value during the variable's creation.
int ejemploVariable;
ejemploVariable = 5;
//Second way
int ejemploVariable = 5;
Both examples will create a variable ejemploVariable
with a numeric value of 5. Personally, I prefer the second way. If you use the first, the compiler will even suggest you use the second, as it is easier to read for humans.
We can define more than one variable if we separate them by commas (,
) and you have to use a semicolon (;
) at the end of each statement.
int ejemploVariable, ejemploVariable2;
1.3 - Implicit variable type
Since C# 3 we can use the implicit type for creating a variable.
This means we don't have to specify the variable type when we declare it, as it will take the type from the assigned value.
var ejemploVariable = 5;
As we see in the example, we use the reserved word var
which will take the type of the value assigned to the variable.
You can do this both with primitive types as well as custom reference types. Personally, I recommend using the explicit type instead of var, as when reading code or doing a code review (git and github course), it's much easier to understand.
1.4 - Declaration of constants
Sometimes, we only need to assign a value to a variable once, as it won't change throughout the runtime of the program. For this, we use what we call a constant. This gives us certain advantages over conventional variables.
- We ensure that this value does not change; in a small project it might not matter, but in a large one it can help a lot.
- Due to the previous point, maintenance is simpler and debugging is easier, as you don't need to search for places where the value might change.
- The compiler is more efficient; it doesn't need to worry about whether or not the value changes, so it can optimize the code knowing the value is always the same.
An example of a constant could be as follows, remember it's a value that never changes.
public const int numeroMeses = 12;
2 - What are operators
Now that we have seen what variables are, let's move to another vital topic: operators. Operators allow us to perform both mathematical and logical operations.
C# has a wide variety of operators as we are about to see.
2.1 - Arithmetic operators
These are 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 just a small example. If you change the operator to any above you will see how it gives 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 allow us to make a comparison. 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 so, it returns true. | ( A > B ) returns true |
< | Checks if the value on the left is less than the one on the right. If so, it returns true. | ( A < B ) returns false |
>= | Checks if the value on the left is greater than or equal to the right. If so, it returns true. | ( A >= B ) returns true |
<= | Checks if the value on the left is less than or equal to the one on the right. If so, it returns true. | ( A <= B ) returns false |
!= | Checks whether both values are equal or not. If they are not equal, returns true. | ( A != B ) returns true |
2.3 - Logical operators
These are logical expressions that compare booleans; For the example A = true, B = false
:
Operator | Description | Example |
&& | Logical AND operator; if both booleans are true, the result will be true. | ( A && B ) returns false |
|| | Logical OR operator; if any of the two is true, the result will be true. | ( A || B ) returns true |
! | Logical NOT operator, used to negate a logical operation; if a condition is true the NOT operator will make it 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 part of the operation. | C = A + B ; Assigns the value of A + B to C |
+= | Arithmetic addition assignment operator; adds the left value to the right one and assigns it. This operation 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