Цілісні типи даних
The int
keyword refers to the "integer" data type which basically represents the integer numbers.
In the last section we learnt the following syntax for declaring a variable:
var variableName = value;
Apart from the above syntax, there is another method of variable declaration:
Here type
refers to the data type of the variable. So far, we know only int
. We can declare a variable of integer data type in the following way:
int myVariable = 10;
In this case, we can also declare a variable without giving it an initial value:
int myVariable;
Note
Giving an initial value to a variable at the time of declaration is called initialization.
The var
keyword allows the compiler to automatically infer the datatype of a variable based on the assigned value. For instance, in var myVariable = 7;
, the variable myVariable
is assigned an integer value, making its data type int
. Such a variable is called an Implicitly-Typed Variable.
Note
When using the
var
keyword, the variable must always be initialized as well, therefore writingvar myVariable;
is invalid.
On the other hand, manually specifying the datatype doesn't require an initial value. Therefore, both int myVariable;
and int myVariable = 10;
are valid statements. In this case, myVariable
is an Explicitly-Typed Variable.
The following code summarizes the above two paragraphs:
main.cs
12345var myVar1 = 10; // Valid var myVar2; // Error: Implicitly-typed variables must be initialized int myVar3 = 10; // Valid int myVar4; // Valid
It is important to note that we cannot use a variable that hasn't been assigned any value. Therefore the following code will give an ERROR:
main.cs
12int myVar; System.Console.WriteLine(myVar); // Error: Use of unassigned local variable 'myVar'
Correction:
main.cs
123int myVar; myVar = 10; System.Console.WriteLine(myVar); // Output: 10
We can reassign a variable as many times as we want:
main.cs
12345678int myVar = 10; System.Console.WriteLine(myVar); // Output: 10 myVar = 15; System.Console.WriteLine(myVar); // Output: 15 myVar = 17; System.Console.WriteLine(myVar); // Output: 17 myVar = 7; System.Console.WriteLine(myVar); // Output: 7
An int
variable can only store values within a certain range. There is another datatype long
which is the same as int
however it can store bigger numbers.
Storing a bigger number than a data type can hold might give unexpected results.
We can perform arithmetic operations on int
and long
data. The following snippet of code shows some examples:
main.cs
1234567891011121314151617181920// int values int a = 10; int b = 25; int c = a + b; // long values long d = 1000; long e = 2500; long f = d + e; // Adding a 'long' and an 'int' always results in a 'long' resultant value. // Therefore we can store the result of 'd + a' into a new 'long' variable called 'g'. long g = d + a; // For the same reason, we cannot store the result of 'd + a' into an 'int' variable int h = d + a; // Error on this line int i = 5 / 2; System.Console.WriteLine(i); // Output: 2 // Dividing 5 by 2 gives 2.5 however since 'int' can only store integer values, hence the decimal (0.5) part is ignored.
There are two other data types uint
and ulong
, called unsigned int and unsigned long respectively. An unsigned data type can only hold positive numbers, consequently they have a bigger positive range.
Дякуємо за ваш відгук!
Запитати АІ
Запитати АІ
Запитайте про що завгодно або спробуйте одне із запропонованих запитань, щоб почати наш чат
Awesome!
Completion rate improved to 1.59
Цілісні типи даних
Свайпніть щоб показати меню
The int
keyword refers to the "integer" data type which basically represents the integer numbers.
In the last section we learnt the following syntax for declaring a variable:
var variableName = value;
Apart from the above syntax, there is another method of variable declaration:
Here type
refers to the data type of the variable. So far, we know only int
. We can declare a variable of integer data type in the following way:
int myVariable = 10;
In this case, we can also declare a variable without giving it an initial value:
int myVariable;
Note
Giving an initial value to a variable at the time of declaration is called initialization.
The var
keyword allows the compiler to automatically infer the datatype of a variable based on the assigned value. For instance, in var myVariable = 7;
, the variable myVariable
is assigned an integer value, making its data type int
. Such a variable is called an Implicitly-Typed Variable.
Note
When using the
var
keyword, the variable must always be initialized as well, therefore writingvar myVariable;
is invalid.
On the other hand, manually specifying the datatype doesn't require an initial value. Therefore, both int myVariable;
and int myVariable = 10;
are valid statements. In this case, myVariable
is an Explicitly-Typed Variable.
The following code summarizes the above two paragraphs:
main.cs
12345var myVar1 = 10; // Valid var myVar2; // Error: Implicitly-typed variables must be initialized int myVar3 = 10; // Valid int myVar4; // Valid
It is important to note that we cannot use a variable that hasn't been assigned any value. Therefore the following code will give an ERROR:
main.cs
12int myVar; System.Console.WriteLine(myVar); // Error: Use of unassigned local variable 'myVar'
Correction:
main.cs
123int myVar; myVar = 10; System.Console.WriteLine(myVar); // Output: 10
We can reassign a variable as many times as we want:
main.cs
12345678int myVar = 10; System.Console.WriteLine(myVar); // Output: 10 myVar = 15; System.Console.WriteLine(myVar); // Output: 15 myVar = 17; System.Console.WriteLine(myVar); // Output: 17 myVar = 7; System.Console.WriteLine(myVar); // Output: 7
An int
variable can only store values within a certain range. There is another datatype long
which is the same as int
however it can store bigger numbers.
Storing a bigger number than a data type can hold might give unexpected results.
We can perform arithmetic operations on int
and long
data. The following snippet of code shows some examples:
main.cs
1234567891011121314151617181920// int values int a = 10; int b = 25; int c = a + b; // long values long d = 1000; long e = 2500; long f = d + e; // Adding a 'long' and an 'int' always results in a 'long' resultant value. // Therefore we can store the result of 'd + a' into a new 'long' variable called 'g'. long g = d + a; // For the same reason, we cannot store the result of 'd + a' into an 'int' variable int h = d + a; // Error on this line int i = 5 / 2; System.Console.WriteLine(i); // Output: 2 // Dividing 5 by 2 gives 2.5 however since 'int' can only store integer values, hence the decimal (0.5) part is ignored.
There are two other data types uint
and ulong
, called unsigned int and unsigned long respectively. An unsigned data type can only hold positive numbers, consequently they have a bigger positive range.
Дякуємо за ваш відгук!