EN
C#/.NET - convert short to string
10
points
In C#/.NET number string can be created in few ways.
1. ToString
example
short value = 123;
string text = value.ToString(); // or 123.ToString()
Console.WriteLine(text); // 123
Output:
123
2. Convert.ToString
example
short value = 123;
string text = Convert.ToString(value);
Console.WriteLine(text); // 123
Output:
123
3. string.Format
example
short value = 123;
string text = string.Format("{0}", value);
Console.WriteLine(text); // 123
Output:
123
4. String interpolation example
short 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
)
short 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. with StringBuilder
class
short value = 123;
string text = new StringBuilder().Append(value).ToString();
Console.WriteLine(text); // 123
Output:
123
7. with TypeConverter
class
TypeConverter converter = TypeDescriptor.GetConverter(typeof(short));
short value = 123;
string text = (string)converter.ConvertTo(value, typeof(string));
Console.WriteLine(text); // 123
Output:
123