Long and String in C#
A long is a data type used to represent integer values that fall outside the range of the int type. It can hold larger numerical values, making it suitable for situations where precision and range matter.
On the other hand, a string is a sequence of characters, typically used to represent textual data. Strings in C# are immutable, meaning that once created, their values cannot be changed.
Method 1: Using ToString()
We will convert a long to a string in C# using the ToString()
method. This method is available on all numeric data types, including longs.
long number = 1234567890123456789;
string str = number.ToString();
Console.WriteLine(str);
Output:
1234567890123456789
Method 2: Using String Interpolation
String interpolation is a convenient feature in C# that allows you to embed expressions directly within string literals. You can use this feature to convert a long to a string easily.
long number = 9876543210987654321;
string str = $"{number}";
Console.WriteLine(str);
Output:
9876543210987654321
Method 3: Using Convert.ToString()
We will also convert a long to a string by using the Convert.ToString()
method. This method accepts any object as input and returns its string representation:
long number = 5555555555555555555;
string str = Convert.ToString(number);
Console.WriteLine(str);
Output:
5555555555555555555
Comments (0)