Languages
[Edit]
EN

C#/.NET - convert long to string

7 points
Created by:
Marcin
15740

1. ToString example

long value = 123;
string text = value.ToString(); // or 123.ToString()
Console.WriteLine(text); // 123

Output:

123

2. Convert.ToString example

long value = 123;
string text = Convert.ToString(value);
Console.WriteLine(text); // 123

Output:

123

3. string.Format example

long value = 123;
string text = string.Format("{0}", value);
Console.WriteLine(text); // 123

Output:

123

4. String interpolation example

long value = 123;
string text = $"{value}";
Console.WriteLine(text); // 123

Output:

123
Note: this feature available is in C# 6 and later versions 

5. Summing string and number example (empty string)

long value = 123;
string text = "" + value;
// string text = value + "";
// string text = string.Empty + value;
// string text = value + string.Empty;
Console.WriteLine(text); // 123

Output:

123

6. StringBuilder example

long value = 123;
string text = new StringBuilder().Append(value).ToString();
Console.WriteLine(text); // 123

Output:

123

7. TypeConverter example

TypeConverter converter = TypeDescriptor.GetConverter(typeof(long));

long value = 123;
string text = (string)converter.ConvertTo(value, typeof(string));
Console.WriteLine(text); // 123

Output:

123
Native Advertising
50 000 ad impressions - 449$
🚀
Get your tech brand or product in front of software developers.
For more information contact us:
Red dot
Dirask - friendly IT community for everyone.

❤️💻 🙂

Join