C#
string conversion
integer conversion
programming tutorial
C# basics

How to convert string to integer in C

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

In C#, converting a string to an integer is simple, but the correct method depends on how much control you need over invalid input. The three common options are int.Parse, int.TryParse, and Convert.ToInt32, and they differ mainly in how they handle bad data and null.

Use int.Parse When the Input Must Be Valid

int.Parse is appropriate when invalid data should be treated as a real error, not as normal control flow.

csharp
1using System;
2
3string text = "42";
4int value = int.Parse(text);
5
6Console.WriteLine(value);

If text contains "abc" or a value outside the Int32 range, int.Parse throws an exception. That is often reasonable for trusted configuration values or internal data formats where failure should stop execution quickly.

The tradeoff is that exceptions are expensive and noisy when bad input is expected, such as data from a user form.

Use int.TryParse for User Input

int.TryParse is the safest default for interactive or external input because it does not throw on failure. Instead, it returns true or false.

csharp
1using System;
2
3string text = "123";
4
5if (int.TryParse(text, out int value))
6{
7    Console.WriteLine($"Parsed value: {value}");
8}
9else
10{
11    Console.WriteLine("Input was not a valid integer.");
12}

This style is better when invalid input is part of normal program behavior. It keeps the code explicit and avoids using exception handling as a branch mechanism.

For example, a console program can keep prompting until the user enters a valid number:

csharp
1using System;
2
3while (true)
4{
5    Console.Write("Enter an integer: ");
6    string input = Console.ReadLine();
7
8    if (int.TryParse(input, out int number))
9    {
10        Console.WriteLine($"You entered {number}");
11        break;
12    }
13
14    Console.WriteLine("Please enter a whole number.");
15}

Understand Convert.ToInt32

Convert.ToInt32 is similar to int.Parse, but it treats null differently.

csharp
1using System;
2
3string input = null;
4int value = Convert.ToInt32(input);
5
6Console.WriteLine(value);

This prints 0 instead of throwing for null. That can be convenient, but it can also hide missing data if you are not expecting that behavior. For non-null invalid strings such as "hello", it still throws a FormatException.

Because of that, Convert.ToInt32 is best used when you intentionally want null to map to zero.

Parsing with Culture and Number Styles

If the input may include leading spaces, signs, or formatting details, use the overloads that accept NumberStyles and a culture.

csharp
1using System;
2using System.Globalization;
3
4string input = "  -15 ";
5bool ok = int.TryParse(
6    input,
7    NumberStyles.Integer,
8    CultureInfo.InvariantCulture,
9    out int value
10);
11
12Console.WriteLine($"{ok} {value}");

This matters when input comes from files, APIs, or user interfaces that may not share the same regional settings as the machine running the code.

Which Method Should You Pick

A practical rule is:

  • use int.TryParse for user input and external text
  • use int.Parse when bad data is exceptional and should fail fast
  • use Convert.ToInt32 only when its null behavior is desirable

The method is not just a syntax preference. It communicates whether invalid input is expected, tolerated, or considered a bug.

Common Pitfalls

  • Using int.Parse on raw user input. A single invalid value turns normal validation into exception handling.
  • Assuming Convert.ToInt32 behaves exactly like int.Parse. null becomes 0, which may not be what you want.
  • Ignoring overflow. Strings representing numbers outside the Int32 range still fail even if they contain only digits.
  • Forgetting about culture and formatting rules when parsing text from external systems.
  • Treating a failed parse as zero without recording the failure. That can silently corrupt business logic.

Summary

  • 'int.TryParse is the safest default for untrusted or interactive input.'
  • 'int.Parse is concise when the input is guaranteed to be valid.'
  • 'Convert.ToInt32 differs mainly in its handling of null.'
  • Parsing behavior can be customized with NumberStyles and culture settings.
  • The right conversion method depends on whether invalid input is expected or exceptional.

Related reading
Course
Intermediate
27 lessons
14 hours
OOD Fundamentals

Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.