Friday, April 30, 2021

C# Examples of out keyword and Parse(), TryParse() & Split() methods

The TryParse method is similar to Parse() method, except the TryParse() method does not throw an exception if the conversion fails. It returns a bool type value which shows whether the parsing was successful in getting the required datatype value from the string. It eliminates the need to use exception handling to test for a FormatException. The TryParse(arg1, arg2) has two arguments while Parse(arg1) uses only one argument.


Example: Find out the numbers from a sentence using TryParse

using System;
using System.Collections.Generic;

//namespace Console_case_with_when_filter_expression
namespace Console_TryParse
{
    class Program
    {
        static void Main(string[] args)
        {
            string sentence = "The year 1984 was a turning point in his life. I won 200000 dollar and then I purchased 10 cars.";
            string[] words = sentence.Split(' ');
            foreach (var word in words)
            {
                bool success = Int32.TryParse(word, out int number);
                if (success)
                {
                    Console.WriteLine(number.ToString());
                }
            }
        }
    }
}

OUTPUT
1984
200000
10

Remark Link
Int32 is an immutable value type that represents signed integers with values that range from negative 2,147,483,648 (which is represented by the Int32.MinValue constant) through positive 2,147,483,647 (which is represented by the Int32.MaxValue constant. .NET also includes an unsigned 32-bit integer value type, UInt32, which represents values that range from 0 to 4,294,967,295.

Example of Parse Method

using System;

namespace Console_Parse_Method
{
    class Program
    {
        static void Main(string[] args)
        {
            string sentence = "The year 1984 was a turning point in his life. I won 200000 dollar and then I purchased 10 cars.";
            string[] words = sentence.Split(' ');
            foreach (var word in words)
            {
                try
                {
                    Console.WriteLine(Int32.Parse(word));
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
                
            }
        }
    }
}

OUTPUT
Input string was not in a correct format.
Input string was not in a correct format.
1984
Input string was not in a correct format.
Input string was not in a correct format.
Input string was not in a correct format.
Input string was not in a correct format.
Input string was not in a correct format.
Input string was not in a correct format.
Input string was not in a correct format.
Input string was not in a correct format.
Input string was not in a correct format.
200000
Input string was not in a correct format.
Input string was not in a correct format.
Input string was not in a correct format.
Input string was not in a correct format.
Input string was not in a correct format.
10
Input string was not in a correct format.

NOTE
Comment //Console.WriteLine(ex.Message); statement
We get output
1984
200000
10



No comments:

Post a Comment

Hot Topics