Convert int to string in C#?

int num = 42; string strNum = num.ToString();

In C#, the int data type represents a signed 32-bit integer. A string, on the other hand, is a sequence of characters. In order to convert an int value to a string value, you can use the ToString method available in C#.

The ToString method is a part of the System namespace and can be called on any instance of a value type or a reference type. When called on an int value, it returns the string representation of that integer.

C# int to a string

Here's an example of converting an int to a string in C#:

int num = 42; string strNum = num.ToString(); Console.WriteLine(strNum); // Output: "42"

Specify a format

You can also specify a format for the string representation using the ToString method. For example, to format an int as a currency value, you can use the following code:

int price = 42; string priceAsString = price.ToString("C"); Console.WriteLine(priceAsString); // Output: "$42.00"

In this example, the format string "C" specifies that the int value should be formatted as a currency value. The resulting string will include the currency symbol, a separator between the dollars and cents, and the number of decimal places specified by the current culture.

The format string "C" specifies that the int value should be formatted as a currency value. The resulting string will include the currency symbol, a separator between the dollars and cents, and the number of decimal places specified by the current culture.