c sharp

C# String to int Conversion

C# is an interesting programming language since it includes a variety of data types to keep developers challenged and interested. To interact with the data and generate instructions that meet the end objective, competence with the data kinds is necessary. A programmer may need to modify the data types while writing to understand how the variable performs within the code — this is termed Type Casting.

To put it another way, computer programmers convert one data type to another for a function to process a variable appropriately. Converting a string-point to an integer is the topic of this article.

There are a few things to keep in mind when converting a string to an integer.

What is Meant by String to int Conversion in C# in Ubuntu 20.04?

Type conversion (or typecasting) transforms a string data type into an integer type. This type of translation is fairly common because we acquire values as strings from inputs, command-line options, and databases, even though the values are integers.

How to Convert String to int in the C# in Ubuntu 20.04?

The Convert class has methods like Convert.oInt32, Int32.Parse, and Int32.TryParse is all functions that can be used to convert a string into an integer. Conversions are performed via these approaches. The source string can be any type of string, including a number, a character combination, or a null string.

When a provided string is a number or a floating-point number, It can be converting it from string to int using any of the methods outlined above, but the pairing of characters and a null string will raise an error, which must be caught using exceptional handling.

Example # 1: Program to Convert a String Into an int by Using the Parse Method in C# in Ubuntu 20.04

The Parse() method transforms a number’s string representation to a 16/32/64 bit signed integer. From here on out, we will solely examine 32-bit conversions for illustration. This method throws a FormatException if the string is not a set of numbers is a minor drawback. Although white spaces at the beginning and end of the string are ignored by Parse(), all characters must have a number value to be converted successfully. To handle the exception, we consider using the try…catch statement. But, here, we know that our string is numeric, so there is no need to try…catch a block.

In the first line of the above source code, we have a library “system” with the keyword “using.” This library provides an interface of the classes and methods of C#. Then, we have defined a namespace library as “Program1”. Within the namespace, we have established a class with the keyword “class” and assigned a name to the class as “square.” Inside our “square” class, we have a static void main function that takes a string[] args as an argument for the string array declaration.

Within the main block, we have a string type variable as “str,” which is stored with the numeric value with double quotes that is the string style representation. Then, we have another variable as “length” of type int. This variable utilizes the parse method. The parse method takes a single argument as “str.” The “str” value will be transformed into an int data type using the parse method. The variable “area” finds the area of the square by implementing the formula (length * length) and will display the area value through the writeLine message.

The parse method gives an integer value in the output as follows.

Example # 2: Program to Convert a String Into an int by Using the TryParse Method in C# in Ubuntu 20.04

The TryParse() methods are provided for all primitive types to convert a string to the requesting data type. Converting a string to an integer should be done in this manner. TryParse() is a good substitute for Parse() because it doesn’t throw an exception when it fails. Instead, if the conversion fails, it just returns False, which simplifies the implementation.

We provided the system library at the beginning of the above source code. The namespace is also specified with the name “Program2”. Then, we created a class called “Add” and invoked the main function from within it. Inside the main function of this class, we have declared a string type variable as “MyStr” and passed a numeric value to it with the string style. Then, we have the other variable “Value” of type int and initialize it with the value “zero.”

After that, we have called the TryParse method, which takes two parameters: the first one is the string variable “MyStr,” and the second one is the int variable “Value” with the keyword “out.” The int variable “Sum” is created, which takes the variable “Value” for the addition operation. Then, we passed the variable “Sum” inside the writeLine method to display.

The string is converted into int type and provides the sum of the integers as follows.

Example # 3: Program to Convert a String Into an int by Using the Convert Method in C# in Ubuntu 20.04

We have used the ConvertToInt32() function here; the only difference between Parse() and ConvertToInt32() is that ConvertToInt32() accepts null values and returns them. As a result, the value is zero. If the string is not numeric, this technique will throw a FormatException. To fix this problem, a try-catch block might be utilized. In this example, we have used exceptional handling, so the try block will raise the exception if it occurs, and the catch block will accept it and write any exception that occurred.

In our third source code, we have again included the system library and the namespace, specified as “Program3”. Then, we have a class called “Circle” inside our namespace block. The class “Circle” has its main public function. The main function of the class has a string variable declaration as “Str_val” and initializes it with the value “null.”

After that, we have to try…catch the definition. Firstly, in the try block, we have called the convert method to which “Str_val” is passed as an argument and assigned to a variable “Diameter.” The other variable, “Radius,” is also defined in the try block, which will return the radius of the circle by dividing the diameter by the value “2”. The circle’s radius will be presented as an integer. Outside the try block, the catch block is implemented for the exception that occurred and prints the message for which the exception occurred.

Hence, the output is zero as we have the string value “null.”


We presented three methods for converting an integer to a string in C# and discussed how to choose between them depending on the source of your input and your confidence in it. Exceptions are costly, and including them in the program flow is not a smart practice. However, they can be handy for extracting critical information, such as the Convert() method’s Overflow condition. All of the strategies listed above are equally effective, but each has its own trade-offs. On the premise of the factors discussed in this article, we propose selecting the best option.

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.