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