---
title: "How to convert string into int in C#?"  
description: "How to convert string into int in C#?"  
author: "Revati S Misra"  
published: 2024-06-11  
updated: 2024-06-12  
canonical: https://www.mindstick.com/forum/160716/how-to-convert-string-into-int-in-c-sharp  
category: "c#"  
tags: ["c#", "string", "datatype"]  
reading_time: 3 minutes  

---

# How to convert string into int in C#?

How to [convert](https://www.mindstick.com/forum/2093/configurationmanager-appsettings-convert-n-to-n-why) `string`into `int`in C#?

## Replies

### Reply by Ashutosh Patel

#### Convert string to int in C#

Converting a string to an integer in C# can be done in many ways depending on the specific needs of your application. Here are general ways to do this.

#### Using int.Parse

The **int.Parse** method is used to convert a string to an integer. Throw a FormatException if the string is not a valid integer.

## Syntax-

```cs
string str = "123";
int number = int.Parse(str);
Console.WriteLine(number); // Output: 123
```

#### using int.TryParse

The **int.TryParse** method attempts to convert a string to an integer and returns a boolean indicating whether the conversion was successful or not. This method is safe because it does not throw an exception if the conversion fails.

## Syntax-

```cs
string str = "123";
bool success = int.TryParse(str, out int number);
if (success)
{
   Console.WriteLine(number); // Output: 123
}
else
{
   Console.WriteLine("Conversion failed.");
}
```

#### Using Convert.ToInt32

The **Convert.ToInt32** method converts the specified string position of any number to a 32-bit equivalent signed integer. It handles null values ​​by returning 0 and throws a FormatException if the string is not a valid integer.

## Syntax-

```cs
string str = "123";
int number = Convert.ToInt32(str);
Console.WriteLine(number); // Output: 123
```

#### Handling exceptions and edge cases

When using **int.Parse** or **Convert.ToInt32**, you must deal with potential exceptions to avoid runtime errors.

## Syntax-

```cs
string strNum = "1234rter";
try
{
  int num = int.Parse(strNum);
  Console.WriteLine("Number is: "+ num);
}
catch (FormatException)
{
   Console.WriteLine("The string format is not a valid for integer.");
}
catch (OverflowException)
{
   Console.WriteLine("The number is too large or too small for an Int32.");
}
```

- There is no need to handle exceptions in the case of `int.tryParse` because it returns the boolean value true or false.
- **int.parse-** Direct convert the values so it throws the exceptions
- **Convert.int32-** it handles null gracefully by returning 0, but throws an exception on invalid format.

## Example-

The below example illustrate the different way to convert string into int in C#

```cs
using System;
// Define a delegate
public delegate void MyDelegate(string message);
public class Program
{
  public static void Main()
   {
       // using int.Parse method
       string strNum1 = "1234";
       int num1 = int.Parse(strNum1);
       Console.WriteLine("Number1 is: "+ num1);

       // using int.TryParse methos
       string strNum2 = "1234xyz";
       bool IsNumber = int.TryParse(strNum2, out int num2);
       if(IsNumber)
       {
           Console.WriteLine("Number2 is: "+ num2);
       }
       else
       {
           Console.WriteLine("Conversion failed: string format not valid for number");
       }

       // using try catch block to handle exception
       string strNum3 = "12345abc";
       try
       {
          int num = int.Parse(strNum3);
          Console.WriteLine("Number is: "+ num);
       }
       catch (FormatException)
       {
           Console.WriteLine("The string format is not a valid for integer.");
       }
       catch (OverflowException)
       {
           Console.WriteLine("The number is too large or too small for an Int32.");
       }
   }
}
```

## Output-

```cs
Number1 is: 1234
Conversion failed: string format not valid for number
The string format is not a valid for integer.
```

**Also, Read:** [What is Boxing and Unboxing in C#?](https://www.mindstick.com/forum/160714/what-is-boxing-and-unboxing-in-c-sharp)


---

Original Source: https://www.mindstick.com/forum/160716/how-to-convert-string-into-int-in-c-sharp

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
