C# | Boolean.ToString() Method

This method is used to convert the value of this instance to its equivalent string representation i.e. either “True” or “False“.

Syntax:

public override string ToString ();

Return Value: This method returns “True” (the value of the TrueString property) if the value of this instance is true, or “False” (the value of the FalseString property) if the value of this instance is false.

Below programs illustrate the use of Boolean.ToString() Method:

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to demonstrate
// Boolean.ToString()
// Method
using System;
  
class GFG {
  
    // Main Method
    public static void Main()
    {
  
        // initalizing the bool variables
        bool cat = false;
        bool dog = true;
  
        // getting the value of string property
        string value1 = cat.ToString();
        string value2 = dog.ToString();
  
        // print the string property
        Console.WriteLine("cat.ToString() returns {0}", value1);
        Console.WriteLine("dog.ToString() returns {0}", value2);
    }
}

chevron_right


Output:

cat.ToString() returns False
dog.ToString() returns True

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to demonstrate
// Boolean.ToString()
// Method
using System;
  
class GFG {
  
    // Main Method
    public static void Main()
    {
  
        // initalizing the bool variables
        bool alpha = false;
        bool beta = true;
        bool gama = true;
        bool delta = false;
        bool theta = true;
  
        // calling getValue() method
        getValue(alpha);
        getValue(beta);
        getValue(gama);
        getValue(delta);
        getValue(theta);
    }
  
    // defining getValue() method
    public static void getValue(bool variable)
    {
  
        // getting the value of string property
        string value = variable.ToString();
  
        // print the string property
        Console.WriteLine("{0}", value);
    }
}

chevron_right


Output:

False
True
True
False
True

Note: XML is case-sensitive, and that the XML specification recognizes “true” and “false” as the valid set of Boolean values. If the string returned by the ToString() method is to be written to an XML file, its String.ToLowerInvariant method should be called first to convert it to lowercase.

Reference:



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.