Whereas doing programming, you might have used many varieties of conversion features within the object-oriented programming languages to transform one sort of variable to a different, particularly in C++ and Python. The C# programming got here up with the “TryParse” methodology from its “System.IO” class to transform any string illustration for numbers, characters, strings, and particular characters to integer or character sort. We may also be discussing which varieties of strings can’t be transformed utilizing the TryParse operate. So, let’s begin with the C# file creation.

Instance # 01:

Let’s take a contemporary begin through the use of the TryParse operate of the “System.IO” class in C# to transform one sort of worth to a different. So, we have now began our code with the C# “System” library utilization. After this, we used the “System.IO” class inside this C# code to make use of the “TryParse” operate. We have now created a easy “Check” class that incorporates a single operate, i.e., the built-in Essential() operate of C#.

This Essential() operate begins from the integer “n” defining at its first line whereas the string “s” has been initialized with a worth “14” as a string. Though 14 is a quantity, it additionally lies within the string class. We have now initialized a Boolean variable “z” that has been taking the parsed “int” worth from the “TryParse” operate of the “IO” class. This TryParse() operate makes use of the string “s” in it to parse it as an int worth to the variable “n” utilizing the key phrase “out” within the parameters.

If the string “s” is efficiently parsed to “int” sort variable “n,” the TryParse() operate will return “True” else “false” to the variable “z” of Boolean sort. The Console.WriteLine() operate assertion is utilized to show the worth of Boolean variable “z” to state whether or not the string might be introduced numerically or not.

After saving our code, we have now compiled it with the C#’s “mcs” compiler on the terminal of Ubuntu 20.04. If the “mcs” compiler is just not put in, strive putting in it first. This compilation will generate a brand new “exe” file on the similar location the place your “cs” file is situated. To execute your already compiled code, it’s good to use this “exe” file on the terminal inside the “mono” runtime instruction. The output exhibits that the string “s” might be introduced as a numeric worth.

Instance # 02:

Inside our second instance, we will probably be taking a look at using the TryParse operate to examine whether or not a particular sort of variable worth might be transformed into a personality, i.e., any character, numbers, particular characters, and so forth. So, we have now began this instance with the “System” library and “System.IO” library utilization. The Check class has a single essential() operate defining a personality variable “c” and Boolean variable “z.”

The TryParse() operate is utilized 4 instances to examine whether or not the “M,” “&,” “145”, and “r” values might be parsed into characters or not. If that’s the case, the true worth will probably be returned to variable “z” in any other case “false.” The Console.WriteLine() operate assertion has been used for every of the values after utilizing the TryParse, to show the worth of a variable “z.” One other Console.WriteLine() operate is used for every worth to transform it to a string and show it within the shell. Now, the code is accomplished right here.

After compiling and operating this code on the shell with “mcs” and “mono” instructions, we have now obtained True for the primary two and the final worth. Whereas for the third worth, it returns False, i.e., Quantity can’t be parsed into a personality sort. The String conversion of every worth can also be displayed on our display screen proven beneath.

Let’s replace the identical code and use different values to examine for the TryParse operate to work. This time we have now been utilizing the “helloWorld,” “<area>,” and “<M>” string sorts within the TryParse operate and saving their Boolean end result inside the “z” variable.

After compiling and operating the above-updated code, we have now obtained False for the primary and third worth from the code whereas the second displayed “True” as an output, i.e., area might be parsed to character sort variable. Whereas a string of multiple character joined with particular operators can’t be transformed. Thus, there isn’t any string illustration of those values as properly.

Instance # 03:

Let’s see if a string of combined characters might be parsed into an integer variable utilizing the TryParse operate of C#. So, we’re utilizing the identical C# code construction and initialize a string variable with the worth of combined characters, i.e., “$%^.” A variable of integer sort “n” and a Boolean sort “z” has been declared. The tryParse operate was used after that to parse the string worth “s” to integer variable “n” and save the returned end result to the “z” variable.

The “if” assertion has been utilized right here to examine if the worth of variable “z” is true or not. If it’s true, You could show the worth of the “n” variable simply parsed by the TryParse operate utilizing the Console.WriteLine() methodology assertion. Else, the Console.WriteLine() operate will show that the worth has not been parsed into an integer utilizing the variable string “s.” This code is able to use now.

After the compilation and execution of the “cs” and “exe” recordsdata, we have now obtained to know that the worth of string “s” can’t be parsed into an integer in C# with the assistance of a TryParse operate.

Instance # 04:

Let’s have our final instance for this tutorial information to utilize the TryParse operate of C# to examine the conversion chance of string to different sorts. This time, we declared a string sort array “Arr” in our C# code and assigned it 8 totally different and combined values. The “foreach” loop has been initialized to iterate the values of this string array taking iterations inside variable “v.”

On every iteration, an integer variable “n” and Boolean variable “z” will probably be initialized, and a TryParse operate will probably be used to parse the present index “v” worth of an array “Arr” to integer variable “n.” If this passing obtained profitable, the Tryparse will return “True” to variable “z” or else return “false.” If the worth of “z” is true, the “if” assertion will proceed to show that the worth of the “v” string has been parsed or transformed into integer “n.” Else half will show that the worth has not been transformed into integer type.

After this code completion, we compiled and executed its “exe” file. It exhibits that the conversion of index 0, 1, 5, 6, and seven values failed, whereas the values at index 2, 3, and 4 indexes have been transformed efficiently.


This information has been crammed with easy examples of using the C# TryParse() operate from its “IO” class. We have now mentioned utilizing TryParse() to transform a string of numbers, particular characters, and combined values to integer or character sort. We have now mentioned how a null or area character can’t be transformed into an integer sort whereas area might be transformed to a ‘character’ sort. In easy phrases, any string with a single character worth might be transformed into a personality sort, and as far as integer sorts might be transformed into “int.”

Did you want this text?
Share it on any of the next social media channels beneath to provide us your vote. Your suggestions helps us enhance.