85

How do I convert a string to an integer in C#?

Raktim Biswas
  • 3,833
  • 5
  • 22
  • 29
user282078
  • 879
  • 1
  • 6
  • 7

12 Answers12

136

If you're sure it'll parse correctly, use

int.Parse(string)

If you're not, use

int i;
bool success = int.TryParse(string, out i);

Caution! In the case below, i will equal 0, not 10 after the TryParse.

int i = 10;
bool failure = int.TryParse("asdf", out i);

This is because TryParse uses an out parameter, not a ref parameter.

k29
  • 571
  • 2
  • 22
Brandon
  • 65,640
  • 30
  • 189
  • 218
  • 2
    @sslaitha, thanks. If it answered your question sufficiently, please remember to mark it as the answer. – Brandon Feb 26 '10 at 20:44
  • 3
    Just note that if you have int i = 10; and use int.TryParse("asdf", out i); that i will contain 0 not 10!!! This is because TryParse uses an out variable, not a ref. – CaffGeek May 03 '10 at 16:12
  • 2
    The "caution" applies to the third example - not the second one. Had me puzzled for a bit since I expected the "this" to refer to the example above, not below. – peter_mcc Feb 06 '14 at 02:19
22
int myInt = System.Convert.ToInt32(myString);

As several others have mentioned, you can also use int.Parse() and int.TryParse().

If you're certain that the string will always be an int:

int myInt = int.Parse(myString);

If you'd like to check whether string is really an int first:

int myInt;
bool isValid = int.TryParse(myString, out myInt); // the out keyword allows the method to essentially "return" a second value
if (isValid)
{
    int plusOne = myInt + 1;
}
devuxer
  • 39,573
  • 46
  • 163
  • 281
11
int a = int.Parse(myString);

or better yet, look into int.TryParse(string)

Raktim Biswas
  • 3,833
  • 5
  • 22
  • 29
Neil N
  • 23,912
  • 15
  • 82
  • 141
6
string varString = "15";
int i = int.Parse(varString);

or

int varI;
string varString = "15";
int.TryParse(varString, out varI);

int.TryParse is safer since if you put something else in varString (for example "fsfdsfs") you would get an exception. By using int.TryParse when string can't be converted into int it will return 0.

MadBoy
  • 10,213
  • 18
  • 88
  • 146
5

If you are sure that you have "real" number in your string, or you are comfortable of any exception that might arise, use this.

string s="4";
int a=int.Parse(s);

For some more control over the process, use

string s="maybe 4";
int a;
if (int.TryParse(s, out a)) {
    // it's int;
}
else {
    // it's no int, and there's no exception;
}
Daniel Mošmondor
  • 19,070
  • 12
  • 53
  • 95
5

Do something like:

var result = Int32.Parse(str);
Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
Tomas Vana
  • 16,537
  • 9
  • 51
  • 62
3
int i;
string whatever;

//Best since no exception raised
int.TryParse(whatever, out i);

//Better use try catch on this one
i = Convert.ToInt32(whatever);
madatanic
  • 1,720
  • 3
  • 16
  • 27
2

4 techniques are benchmarked here.

The fastest way turned out to be the following:

y = 0;
for (int i = 0; i < s.Length; i++)
       y = y * 10 + (s[i] - '0');

"s" is your string that you want converted to an int. This code assumes you won't have any exceptions during the conversion. So if you know your string data will always be some sort of int value, the above code is the best way to go for pure speed.

At the end, "y" will have your int value.

2
bool result = Int32.TryParse(someString, out someNumeric)

This method will try to convert someString into someNumeric, and return a result depends if the conversion is successful, true if conversion is successful and false if conversion failed. Take note that this method will not throw exception if the conversion failed like how Int32.Parse method did and instead it returns zero for someNumeric.

For more information, you can read here:

https://msdn.microsoft.com/en-us/library/f02979c7(v=vs.110).aspx?cs-save-lang=1&cs-lang=csharp#code-snippet-2
&
How to convert string to integer in C#

jet_choong
  • 298
  • 2
  • 9
0
class MyMath
{
    public dynamic Sum(dynamic x, dynamic y)
    {
        return (x+y);
    }
}

class Demo
{
    static void Main(string[] args)
    {
        MyMath d = new MyMath();
        Console.WriteLine(d.Sum(23.2, 32.2));
    }
}
Tshilidzi Mudau
  • 5,472
  • 6
  • 32
  • 44
Abdo
  • 1
0
int i;

string result = Something;

i = Convert.ToInt32(result);
abatishchev
  • 92,232
  • 78
  • 284
  • 421
deepu
  • 1,797
  • 6
  • 38
  • 63
0

You can use either,

    int i = Convert.ToInt32(myString);

or

    int i =int.Parse(myString);
Deadlock
  • 3,375
  • 1
  • 17
  • 24