How do I convert a string to an integer in C#?
-
1possible duplicate of http://stackoverflow.com/questions/1019793/how-can-i-convert-string-to-int – John Rasch May 03 '10 at 16:43
12 Answers
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.
-
2@sslaitha, thanks. If it answered your question sufficiently, please remember to mark it as the answer. – Brandon Feb 26 '10 at 20:44
-
3Just 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
-
2The "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
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;
}

- 41,681
- 47
- 180
- 292
int a = int.Parse(myString);
or better yet, look into int.TryParse(string)

- 4,011
- 5
- 27
- 32

- 24,862
- 16
- 85
- 145
Do something like:
var result = Int32.Parse(str);

- 30,738
- 21
- 105
- 131

- 18,317
- 9
- 53
- 64
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
.

- 10,824
- 24
- 95
- 156
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;
}

- 19,718
- 12
- 58
- 99
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.
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);

- 1,760
- 3
- 16
- 28
bool result = Int32.TryParse(someString, out someNumeric)
This method will try to convert someString
into someNumeric
, and return a result
depending on whether or not the conversion is successful: true
if conversion is successful and false
if conversion failed. Take note that this method will not throw an exception if the conversion failed like how Int32.Parse
method did and instead 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#

- 1,050
- 1
- 9
- 27

- 382
- 3
- 13
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));
}
}

- 7,373
- 6
- 36
- 49

- 1
int i;
string result = Something;
i = Convert.ToInt32(result);

- 98,240
- 88
- 296
- 433

- 1,993
- 6
- 42
- 64
You can use either,
int i = Convert.ToInt32(myString);
or
int i =int.Parse(myString);

- 4,211
- 1
- 20
- 25