Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Basic Type Conversion | Dealing with Data Types
C# Basics

Basic Type ConversionBasic Type Conversion

We learnt about Type Casting in the last chapter however we cannot deal with strings in type casting.

For-example, if we have a string which represents a number such as "1234", and we want to perform arithmetic operations on it, we cannot do that without converting it into a numerical data type. To do that we can use a method called Type Conversion.

Type Conversion can be done using a relevant Convert method. We will explore methods in detail in later sections however for understanding purposes, methods are essentially commands that tell the computer to perform a certain operation.

We can convert a string to an int using the Convert.ToInt32() method. The syntax of the method is following:

cs

main.cs

Example:

cs

main.cs

This method takes in a value, converts it into an integer if possible, and returns that value in integer form which we can either store in variables or display directly:

cs

main.cs

Note that the string must contain an integer number in the correct format which means there must be no extra spaces or symbols in the value, otherwise it will show an error:

cs

main.cs

Another point to note that any kind of value can be passed into the Convert function as long as it can logically be converted to an int:

cs

main.cs

To convert an int to string, we can use the Convert.ToString() method:

cs

main.cs

Following is a list of the commonly used Convert methods:

MethodOperation
Convert.ToInt32()Convert a value to an integer
Convert.ToInt64()Convert a value to long
Convert.ToDouble()Convert a value to double
Convert.ToString()Convert a value to string

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

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

Зміст курсу

C# Basics

Basic Type ConversionBasic Type Conversion

We learnt about Type Casting in the last chapter however we cannot deal with strings in type casting.

For-example, if we have a string which represents a number such as "1234", and we want to perform arithmetic operations on it, we cannot do that without converting it into a numerical data type. To do that we can use a method called Type Conversion.

Type Conversion can be done using a relevant Convert method. We will explore methods in detail in later sections however for understanding purposes, methods are essentially commands that tell the computer to perform a certain operation.

We can convert a string to an int using the Convert.ToInt32() method. The syntax of the method is following:

cs

main.cs

Example:

cs

main.cs

This method takes in a value, converts it into an integer if possible, and returns that value in integer form which we can either store in variables or display directly:

cs

main.cs

Note that the string must contain an integer number in the correct format which means there must be no extra spaces or symbols in the value, otherwise it will show an error:

cs

main.cs

Another point to note that any kind of value can be passed into the Convert function as long as it can logically be converted to an int:

cs

main.cs

To convert an int to string, we can use the Convert.ToString() method:

cs

main.cs

Following is a list of the commonly used Convert methods:

MethodOperation
Convert.ToInt32()Convert a value to an integer
Convert.ToInt64()Convert a value to long
Convert.ToDouble()Convert a value to double
Convert.ToString()Convert a value to string

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

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