EN
C#/.NET - convert string to int
11 points
In C#/.NET string can be parsed to int in few ways.
xxxxxxxxxx
1
string text = "123";
2
int value = int.Parse(text);
3
Console.WriteLine(value);
Output:
xxxxxxxxxx
1
123
xxxxxxxxxx
1
string text = "123";
2
int value;
3
4
if (int.TryParse(text, out value))
5
Console.WriteLine(value);
Output:
xxxxxxxxxx
1
123
xxxxxxxxxx
1
string text = "123";
2
int value = Convert.ToInt32(text);
3
Console.WriteLine(value);
Output:
xxxxxxxxxx
1
123
xxxxxxxxxx
1
TypeConverter converter = TypeDescriptor.GetConverter(typeof(int));
2
3
string text = "123";
4
int value = (int)converter.ConvertFrom(text);
5
6
Console.WriteLine(value);
Output:
xxxxxxxxxx
1
123
Check this link: https://dirask.com/q/QD9EaD
The following methodsint.Parse
,Convert.ToInt32
andTypeConverter.ConvertFrom
throw System.FormatException.