w3resource

C# Sharp Exercises: Equality comparison between multiple parameters

C# Sharp Basic: Exercise-100 with Solution

Write a C# Sharp program to check the equality comparison (value and type ) of two parameters. Return true if they are equal otherwise false.

Sample Data:
("AAA", "BBB") -> False
(true, false) -> False
(true, "true") -> False
(10, 10) -> True

Sample Solution-1:

C# Sharp Code:

using System;
using System.Text.RegularExpressions;
namespace exercises
{
    class Program
    {
        static void Main(string[] args)
        {
            string text1 = "AAA";
            string text2 = "BBB";
            Console.WriteLine("Original values: " + text1+", "+text2);
            Console.WriteLine("Check the equality of the said values: " + test(text1,text2));
            Boolean v1 = true;
            Boolean v2 = false;
            Console.WriteLine("Original values: " + v1 + ", " + v2);
            Console.WriteLine("Check the equality of the said values: " + test(v1, v2));
            Boolean v3 = true;
            string text3 = "true";
            Console.WriteLine("Original values: " + v1 + ", " + text3);
            Console.WriteLine("Check the equality of the said values: " + test(v1, v2));
            int n1 = 10;
            int n2 =  10;
            Console.WriteLine("Original values: " + n1 + ", " + n2);
            Console.WriteLine("Check the equality of the said values: " + test(n1, n2));
        }

        public static bool test(object x, object y)
        {
            if (!x.Equals(y))
            {
                return false;
            }

            if (!x.GetType().Equals(y.GetType()))
            {
                return false;
            }
            return true;
        }
    }
}

Sample Output:

Original values: AAA, BBB
Check the equality of the said values: False
Original values: True, False
Check the equality of the said values: False
Original values: True, true
Check the equality of the said values: False
Original values: 10, 10
Check the equality of the said values: True

Flowchart:

Flowchart: C# Sharp Exercises - Equality comparison between multiple parameters.

Sample Solution-2:

C# Sharp Code:

 using System;
using System.Text.RegularExpressions;
namespace exercises
{
    class Program
    {
        static void Main(string[] args)
        {
            string text1 = "AAA";
            string text2 = "BBB";
            Console.WriteLine("Original values: " + text1+", "+text2);
            Console.WriteLine("Check the equality of the said values: " + test(text1,text2));
            Boolean v1 = true;
            Boolean v2 = false;
            Console.WriteLine("Original values: " + v1 + ", " + v2);
            Console.WriteLine("Check the equality of the said values: " + test(v1, v2));
            Boolean v3 = true;
            string text3 = "true";
            Console.WriteLine("Original values: " + v1 + ", " + v2);
            Console.WriteLine("Check the equality of the said values: " + test(v1, v2));
            int n1 = 10;
            int n2 =  10;
            Console.WriteLine("Original values: " + n1 + ", " + n2);
            Console.WriteLine("Check the equality of the said values: " + test(n1, n2));
        }

        public static bool test(object x, object y)
        {
            return x.Equals(y);
        }
    }
}

Sample Output:

Original values: AAA, BBB
Check the equality of the said values: False
Original values: True, False
Check the equality of the said values: False
Original values: True, False
Check the equality of the said values: False
Original values: 10, 10
Check the equality of the said values: True

Flowchart:

Flowchart: C# Sharp Exercises - Equality comparison between multiple parameters.

C# Sharp Code Editor:

Improve this sample solution and post your code through Disqus

Previous C# Sharp Exercise: Prime number in strictly ascending decimal digit order.
Next C# Sharp Exercise: Calculate the value of e (Euler's number).

What is the difficulty level of this exercise?



Share this Tutorial / Exercise on : Facebook and Twitter