Related Articles

Related Articles

C# | Boolean.Equals(Boolean) Method
  • Last Updated : 02 May, 2019

This method is used to return a value indicating whether this instance is equal to a specified Boolean object.

Syntax:

public bool Equals (bool obj);

Here, obj is a boolean value to compare to this instance.

Return Value: This method returns true if obj has the same value as this instance otherwise it returns false.

Below programs illustrate the use of Boolean.Equals(bool obj) Method:



Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to demonstrate
// Boolean.Parse(String)
// Method
using System;
  
class GFG {
  
    // Main Method
    public static void Main()
    {
  
        // passing different values
        // to the method to check
        check(true, true);
        check(true, false);
        check(false, true);
        check(false, false);
    }
  
    // Defining check method
    public static void check(bool input1, bool input2)
    {
  
        // declaring bool variable
        bool val;
  
        // getting parsed value
        val = input1.Equals(input2);
  
        // cheking the equivalency
        if (val == true)
            Console.WriteLine("{0} is equal to {1}",
                                    input1, input2);
  
        else
            Console.WriteLine("{0} is not equal to {1}",
                                        input1, input2);
    }
}

chevron_right


Output:

True is equal to True
True is not equal to False
False is not equal to True
False is equal to False

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to demonstrate
// Boolean.Parse(String)
// Method
using System;
  
class GFG {
  
    // Main Method
    public static void Main()
    {
  
        // Declaring the variable 
        // input1 and input2
        bool input1, input2;
  
        // initializing the variables
        input1 = true;
        input2 = false;
  
        // cheking the equality
        bool val = input1.Equals(input2);
  
        // cheking the equivalency
        if (val == true)
            Console.WriteLine("input1 is equal to input2");
  
        else
            Console.WriteLine("input1 is not equal to input2");
    }
}

chevron_right


Output:

input1 is not equal to input2

Note: This method implements the System.IEquatable<T> interface, and performs slightly better than Equals because it does not have to convert the obj parameter to an object.

Reference:




My Personal Notes arrow_drop_up
Recommended Articles
Page :