Skip to content
Related Articles

Related Articles

Improve Article

C# | Boolean.ToString() Method

  • Last Updated : 04 Oct, 2021
Geek Week

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: 



C#




// C# program to demonstrate
// Boolean.ToString()
// Method
using System;
 
class GFG {
 
    // Main Method
    public static void Main()
    {
 
        // initializing 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);
    }
}
Output: 
cat.ToString() returns False
dog.ToString() returns True

 

Example 2:

C#




// C# program to demonstrate
// Boolean.ToString()
// Method
using System;
 
class GFG {
 
    // Main Method
    public static void Main()
    {
 
        // initializing the bool variables
        bool alpha = false;
        bool beta = true;
        bool gamma = true;
        bool delta = false;
        bool theta = true;
 
        // calling getValue() method
        getValue(alpha);
        getValue(beta);
        getValue(gamma);
        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);
    }
}
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
Recommended Articles
Page :