Search This Blog

Sunday 1 September 2019

Difference between Parse and TryParse in C#

Hi C# Buddies,

Hope you are are doing great.

This is the first article that I am writing in C#. Hope you are going to enjoy the article.

As a beginner, one would like to know how to convert from one data type to another in C#.

There are several ways of doing it, based on our requirement we choose the option that best fits.

In our example, we will see, how to convert a string into a decimal number. The same logic applies to other data types that are available.

NB:- In all my examples that we are going to discuss, I will be using Console application for practice purpose. Some times the code is not optimized as we need to elaborate on the concept.

1st Approach:-
    class Program
    {
        static void Main()
        {
            try
            {
                //try to change the above string to "100,99" , "100A", "100e1", "-100". Make observation of output.
                string amountString = "100,09";

                //Convert.ToDecimal method converts the given string into decimal. If the string is a valid number then we get output. If the given string is not a valid number then the program crashes(This is the drawback of the Convert.ToDecimal Method)
                Console.WriteLine(Convert.ToDecimal(amountString));
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            Console.ReadLine();
        }
    }

2nd Approach:-
    class Program
    {
        static void Main()
        {
            try
            {
                //try to change the below amountString to "100.99" , "100A", "10e3", "-100". Make observation of output.
                string amountString = "100,99";

                //Parse converts the given string into decimal. If the string is a valid number then we get output. If the given string is not a valid number then the program crashes(This is the drawback of the Parse Method)
                decimal amount = decimal.Parse(amountString);
                Console.WriteLine(amount);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            Console.ReadLine();
        }
    }

Note:- To overcome the drawback of Parse and Convert.ToDecimal Methods we use TryParse.

3rd Approach:-
    class Program
    {
        static void Main()
        {
            string amountString = "100,99";
            //Once you get the output with "100,99" then try "100.99". Make overserveration of output.
            decimal amount;
            //TryParse tries to convert the given string into decimal. If the string is valid decimal then we get output. If the given string is not a valid decimal then the result is zero(0)
            decimal.TryParse(amountString, out amount);
            Console.WriteLine(amount);
            Console.ReadLine();
        }
    }

Hope you have enjoyed the article. Have a great day. See you in the next article.

Please comment and share and ask doubts if there is anything that is not clear or if you need me to explain a specific concept in C#.

Difference between Parse and TryParse in C#

Hi C# Buddies, Hope you are are doing great. This is the first article that I am writing in C#. Hope you are going to enjoy the article...