Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Integer Data Types | Dealing with Data Types
course content

Зміст курсу

C# Basics

Integer Data TypesInteger Data Types

Data Types are classification of data values into different categories based on their nature such as integer numbers, decimal numbers, textual data etc. Each data type has different kinds of operations associated with it, for-example we can perform arithmetic operations on numerical data but not on textual data.

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:

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:

In this case we can also declare a variable without giving it an initial value:

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 that when using the var keyword, the variable must always be initialized as well, therefore writing var 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.

Following code summarizes the above two paragraphs:

cs

main.cs

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:

cs

main.cs

Correction:

cs

main.cs

We can reassign a variable as many times as we want:

cs

main.cs

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.

Range
int-2,147,483,648 to 2,147,483,647
long-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807

Storing a bigger number than a data type can hold might give unexpected results. We don't need to look at that into detail at this level.

We can perform arithmetic operations on int and long data. The following snippet of code shows some examples:

cs

main.cs

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.

Range
uint0 to 4,294,967,295
ulong0 to 18,446,744,073,709,551,615

Which one of the following datatypes is suitable for storing a large positive integer value?

Виберіть правильну відповідь

Все було зрозуміло?

Секція 2. Розділ 1
course content

Зміст курсу

C# Basics

Integer Data TypesInteger Data Types

Data Types are classification of data values into different categories based on their nature such as integer numbers, decimal numbers, textual data etc. Each data type has different kinds of operations associated with it, for-example we can perform arithmetic operations on numerical data but not on textual data.

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:

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:

In this case we can also declare a variable without giving it an initial value:

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 that when using the var keyword, the variable must always be initialized as well, therefore writing var 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.

Following code summarizes the above two paragraphs:

cs

main.cs

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:

cs

main.cs

Correction:

cs

main.cs

We can reassign a variable as many times as we want:

cs

main.cs

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.

Range
int-2,147,483,648 to 2,147,483,647
long-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807

Storing a bigger number than a data type can hold might give unexpected results. We don't need to look at that into detail at this level.

We can perform arithmetic operations on int and long data. The following snippet of code shows some examples:

cs

main.cs

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.

Range
uint0 to 4,294,967,295
ulong0 to 18,446,744,073,709,551,615

Which one of the following datatypes is suitable for storing a large positive integer value?

Виберіть правильну відповідь

Все було зрозуміло?

Секція 2. Розділ 1
some-alt