Example # 01:
Let’s take a fresh start by using the TryParse function of the “System.IO” class in C# to convert one type of value to another. So, we have started our code with the C# “System” library utilization. After this, we used the “System.IO” class within this C# code to use the “TryParse” function. We have created a simple “Test” class that contains a single function, i.e., the built-in Main() function of C#.
This Main() function starts from the integer “n” defining at its first line while the string “s” has been initialized with a value “14” as a string. Although 14 is a number, it also lies in the string category. We have initialized a Boolean variable “z” that has been taking the parsed “int” value from the “TryParse” function of the “IO” class. This TryParse() function uses the string “s” in it to parse it as an int value to the variable “n” using the keyword “out” in the parameters.
If the string “s” is successfully parsed to “int” type variable “n,” the TryParse() function will return “True” else “false” to the variable “z” of Boolean type. The Console.WriteLine() function statement is utilized to display the value of Boolean variable “z” to state whether the string can be presented numerically or not.
After saving our code, we have compiled it with the C#’s “mcs” compiler at the terminal of Ubuntu 20.04. If the “mcs” compiler is not installed, try installing it first. This compilation will generate a new “exe” file at the same location where your “cs” file is located. To execute your already compiled code, you need to use this “exe” file at the terminal within the “mono” runtime instruction. The output shows that the string “s” can be presented as a numeric value.
Example # 02:
Within our second example, we will be looking at the use of the TryParse function to check whether a specific type of variable value can be converted into a character, i.e., any character, numbers, special characters, etc. So, we have started this example with the “System” library and “System.IO” library usage. The Test class has a single main() function defining a character variable “c” and Boolean variable “z.”
The TryParse() function is utilized 4 times to check whether the “M,” “&,” “145”, and “r” values can be parsed into characters or not. If so, the true value will be returned to variable “z” otherwise “false.” The Console.WriteLine() function statement has been used for each of the values after using the TryParse, to display the value of a variable “z.” Another Console.WriteLine() function is used for each value to convert it to a string and display it in the shell. Now, the code is completed here.
After compiling and running this code on the shell with “mcs” and “mono” commands, we have got True for the first two and the last value. While for the third value, it returns False, i.e., Number cannot be parsed into a character type. The String conversion of each value is also displayed on our screen shown below.
Let’s update the same code and use other values to check for the TryParse function to work. This time we have been using the “helloWorld,” “<space>,” and “<M>” string types in the TryParse function and saving their Boolean result within the “z” variable.
After compiling and running the above-updated code, we have got False for the first and third value from the code while the second one displayed “True” as an output, i.e., space can be parsed to character type variable. While a string of more than one character joined with special operators cannot be converted. Thus, there is no string representation of these values as well.
Example # 03:
Let’s see if a string of mixed characters can be parsed into an integer variable using the TryParse function of C#. So, we are using the same C# code structure and initialize a string variable with the value of mixed characters, i.e., “$%^.” A variable of integer type “n” and a Boolean type “z” has been declared. The tryParse function was used after that to parse the string value “s” to integer variable “n” and save the returned result to the “z” variable.
The “if” statement has been utilized here to check if the value of variable “z” is true or not. If it’s true, You need to display the value of the “n” variable just parsed by the TryParse function using the Console.WriteLine() method statement. Else, the Console.WriteLine() function will display that the value has not been parsed into an integer using the variable string “s.” This code is ready to use now.
After the compilation and execution of the “cs” and “exe” files, we have got to know that the value of string “s” cannot be parsed into an integer in C# with the help of a TryParse function.
Example # 04:
Let’s have our last example for this tutorial guide to make use of the TryParse function of C# to check the conversion possibility of string to other types. This time, we declared a string type array “Arr” in our C# code and assigned it 8 different and mixed values. The “foreach” loop has been initialized to iterate the values of this string array taking iterations within variable “v.”
On each iteration, an integer variable “n” and Boolean variable “z” will be initialized, and a TryParse function will be used to parse the current index “v” value of an array “Arr” to integer variable “n.” If this passing got successful, the Tryparse will return “True” to variable “z” or else return “false.” If the value of “z” is true, the “if” statement will continue to display that the value of the “v” string has been parsed or converted into integer “n.” Else part will display that the value has not been converted into integer form.
After this code completion, we compiled and executed its “exe” file. It shows that the conversion of index 0, 1, 5, 6, and 7 values failed, while the values at index 2, 3, and 4 indexes have been converted successfully.
This guide has been filled with simple examples of utilizing the C# TryParse() function from its “IO” class. We have discussed using TryParse() to convert a string of numbers, special characters, and mixed values to integer or character type. We have discussed how a null or space character cannot be converted into an integer type while space can be converted to a ‘character’ type. In simple words, any string with a single character value can be converted into a character type, and so far as integer types can be converted into “int.”