Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
C# program to convert string to long
Converting a string to a long data type in C# can be accomplished using several methods. The most common approaches are long.Parse(), Convert.ToInt64(), and long.TryParse() for safe conversion.
Syntax
Following are the different syntaxes for converting string to long −
long result = long.Parse(stringValue);
long result = Convert.ToInt64(stringValue);
bool success = long.TryParse(stringValue, out long result);
Using long.Parse() Method
The long.Parse() method converts a string representation of a number to its 64-bit signed integer equivalent −
using System;
class Demo {
static void Main() {
string str = "6987646475767";
long res = long.Parse(str);
Console.WriteLine("Original string: " + str);
Console.WriteLine("Converted to long: " + res);
Console.WriteLine("Type: " + res.GetType());
}
}
The output of the above code is −
Original string: 6987646475767 Converted to long: 6987646475767 Type: System.Int64
Using Convert.ToInt64() Method
The Convert.ToInt64() method provides another way to convert a string to long and handles null values gracefully −
using System;
class Demo {
static void Main() {
string str1 = "9223372036854775807"; // Max long value
string str2 = "-9223372036854775808"; // Min long value
long result1 = Convert.ToInt64(str1);
long result2 = Convert.ToInt64(str2);
Console.WriteLine("Max long value: " + result1);
Console.WriteLine("Min long value: " + result2);
}
}
The output of the above code is −
Max long value: 9223372036854775807 Min long value: -9223372036854775808
Using long.TryParse() for Safe Conversion
The long.TryParse() method attempts to convert a string without throwing an exception if the conversion fails −
using System;
class Demo {
static void Main() {
string validStr = "123456789";
string invalidStr = "abc123";
// Safe conversion with TryParse
if (long.TryParse(validStr, out long result1)) {
Console.WriteLine("Valid conversion: " + result1);
}
if (long.TryParse(invalidStr, out long result2)) {
Console.WriteLine("Valid conversion: " + result2);
} else {
Console.WriteLine("Invalid string cannot be converted to long");
}
}
}
The output of the above code is −
Valid conversion: 123456789 Invalid string cannot be converted to long
Comparison of Methods
| Method | Exception Handling | Null Value Handling | Performance |
|---|---|---|---|
| long.Parse() | Throws exception on invalid input | Throws exception on null | Fast |
| Convert.ToInt64() | Throws exception on invalid input | Returns 0 on null | Slightly slower |
| long.TryParse() | Returns false on invalid input | Returns false on null | Fast and safe |
Conclusion
Converting strings to long in C# can be done using long.Parse(), Convert.ToInt64(), or long.TryParse(). Use long.TryParse() when you need safe conversion without exceptions, and long.Parse() when you're certain the string contains a valid number.
