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
course content

Course Content

C# Basics

C# Basics

1. Getting Started
2. Dealing with Data Types
3. Control Structures
4. Loops
5. Arrays
6. Methods

book
Basic Type Conversion

In the previous chapter, we learned about type casting, but it doesn't work with strings.

Imagine you have a string like "1234" that represents a number, and you want to do math with it. You can't do that until you change it into a number type. This is where Type Conversion comes in handy.

Type conversion is done using specific Convert methods. We'll dive deeper into these methods later, but for now, think of them as instructions that tell the computer to do something specific.

To change a string to an int, you can use the Convert.ToInt32() method. Here's how it looks in code:

cs

main

copy
1
Convert.ToInt32(dataToConvert);

Example:

cs

main

copy
1
Convert.ToInt32("12345");

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

copy
1234567891011121314
using System; namespace ConsoleApp { internal class Program { static void Main(string[] args) { int val = Convert.ToInt32("12345"); Console.WriteLine(val); Console.WriteLine(Convert.ToInt32("67890")); } } }

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

copy
123
Convert.ToInt32("3.14"); // Error Convert.ToInt32(""); // Error Convert.ToInt32("30,000"); // Error

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

copy
1234567891011121314
using System; namespace ConsoleApp { internal class Program { static void Main(string[] args) { int val = Convert.ToInt32(1234.567); Console.WriteLine(val); // The value is rounded to the nearest integer. } } }

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

cs

main

copy
1234567891011121314
using System; namespace ConsoleApp { internal class Program { static void Main(string[] args) { int number = 1234567; string text = Convert.ToString(number); Console.WriteLine(text); // Output: 1234567 } } }

Following is a list of the commonly used Convert methods:

1. What is the purpose of type conversion in programming, and why is it necessary when dealing with strings that represent numbers?
2. Which method would you use to convert a string containing a number, such as "1234", into an integer in C#?
What is the purpose of type conversion in programming, and why is it necessary when dealing with strings that represent numbers?

What is the purpose of type conversion in programming, and why is it necessary when dealing with strings that represent numbers?

Select the correct answer

Which method would you use to convert a string containing a number, such as "1234", into an integer in C#?

Which method would you use to convert a string containing a number, such as "1234", into an integer in C#?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 2. Chapter 13
We're sorry to hear that something went wrong. What happened?
some-alt