c sharp

C# Int to String

C sharp programming language uses every data type variable that is used by other programming languages. These data types need to be converted into each other according to the condition of the program. Similarly, the integer values are converted into a string data type. String data type variables are capable of storing all types of data in the form of characters either an alphanumeric value, a symbol, or a blank space. There can be several approaches used to convert data types. We have used a few of the basic and easily implemented examples in the tutorial.

Example 1: ToString() Method

The first example is to calculate the conversion of an integer into a string. We will take an integer value and the resultant value will be a string. But first, we will introduce the library essential for the execution. This is the ‘system’ header file that enables the code to get information about the system. This header file contains all information regarding classes, structures, data types, etc.

Using system;

Then, we will declare a class having the main program. As a string data variable is indulged with the usage of several built-in functions. These functions are used for different purposes. So, for the conversion of integers to the string, we will use a built-in function of string that is the ‘Tostring()’ function. First, we will declare a string-type variable that will store the resultant string value. Another integer type variable is declared and is assigned with the value that we want to convert.

stringTonumber = number.Tostring();

The function is called by the number variable that we want to convert. The integer value will be converted into a string by doing so.

After conversion, the resultant value will be displayed through the ‘console.wireline’ statement using the variable directly for displaying the value. The file is closed and we will save it with the ‘.cs’ extension, being a C sharp programming source code.

For the implementation in the Linux Ubuntu system, every programming language requires a compiler, hence we have used MCS to compile the code, whereas ‘Mono’ will be used to execute the file with the extension of ‘.exe’.

$ MCS file.cs

$ mono file.exe

You can see that the value is obtained. By appearance, the numeric value is the same, but the data type is changed. The integer data type has only the numeric digits, but after converting into a string, the value contains characters. And characters can contain both alpha-numeric values.

Example 2: Int32.Tostring()

This function takes the value and converts the non-decimal value into the equivalent string characters. This function is different from the simple conversion as we have described in the previous example. This function uses a specific format and information regarding the culture. Inside the main function, the Int32.MaxValue is taken. That is not a numeric value but contains the “culture” info regarding the string. So to work properly, we need an object of CultureInfo to initialize the object for the conversion.

CultureInfo provider = new CultureInfo(“fr – FR”);

Here, the function takes a statement of the culture of a string that enables the conversion. A string variable has several cultures for each conversion of data type, so for int to string, we need this one. The format of conversion is also declared here for the non-decimal value.

String format = ‘D8’;

Both the provider and the format is used to be the parameter in the function at the time of conversion.

String str = number.Tostring(format , provider);

The string will be created by following the upper-defined steps. A long string of integers is obtained. Like int32, int16 also plays the same role by following a different format and the provider.

Example 3: Concatenation

In strings, concatenation is the phenomenon in which two strings are added. Just like that, a ‘+’ operator is used to change an int into a string. This is a concatenate operation.  By using this method, we can also convert the integer value into a string. The basic syntax followed for using this operator is by having two strings that are joined by having the number between the string. This number is the integer and is occupied by the ‘+’ plus operator on both sides.

string output = “string1″ + number + ” string2″;

Let us use this approach in the source code. A number is assigned to the integer value. Then we will use the console.writeline statement for displaying the results. Within this statement, both strings will be combined by the variable.

“converting into a string by using concatenate operation(+) result is “+number+”\n and now it becomes the string”

The operator will bind the variable with the strings on the left and right sides.

However, the concatenation process can also be a confirmation for the values in the variable to assure that the value is a string. Because this concatenation is applied on the strings only.

On execution, the numeric value after the conversion is embedded between two strings becoming the part of a string.

Example 4: StringBuilder

Another method of converting int to string is through a StringBuilder. It is used to perform the mutability of the characters in the strings. Inside the main program, we will initialize the integer number. A StringBuilder object is created through which we will do the conversion.

Var stringBuilder = nw stringBuilder();

An append() function is used to add the value of an integer to the StringBuilder so that the value becomes the string.Append() is a built-in function that is used to add the values at the end of already having a string in the variable. So for this purpose, StringBuilder plays a role, as the name indicates it will add the values.

StringBuilder. Append(number);

By using this Stringbuilder, the value will be displayed.

Example 5: String.Format()

Another method used for the conversion is through the ‘string.format()’ function. This function takes the integer value as a parameter and then converts it into the string. A string-type data variable is declared to store the resultant values. We will first initialize the integer value. And then use this variable in the string.format() function.

String outputstring = string.Format ( +number);

The value in the integer will be a string after a successful conversion.

Additional information

Convert.tostring() is a function that is used in the conversion of an integer into a string, as compared to the above-mentioned functions, as it directly converts the value in a string and other data types as well. For example from the conversion of string to integer, we can use this function after customization like Convert.ToInt().


Strings use built-in functions for the conversion of any data type towards them. These functions are included in the system library of the C sharp programming language. Few of them are explained with examples implemented in Ubuntu 20.04. As an input, an integer value is taken and it is converted into strings through tostring(), Int32, concatenate operator, or through the StringBuilder(). Every function is executed through the object that is created to be initialized. Each function generates the value in strings by taking the integer value as a parameter. All these functions of strings are easily implemented in any programming language.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.