Sruti Ranjan Majhi
Sruti Ranjan Majhi

Reputation: 11

How to get the bool variable in the default case i.e "false"

I have the following method:

    private String getLicenseID(String arglicenseID, bool istrue)
    {
        String licenseID = arglicenseID;

        if (licenseID != null)
        {
            licenseID =  licenseID + " " + istrue;
        }
        return licenseID;
    }

When I call the function I pass istrue as false.

The problem is that while debugging, istrue has the value "false" (note that "f" is lowercase), but when I inspect the returned value of licenseID, I see the licenseID concatenated with value "False" ("F" is uppercase).

My requirement is to get licenseID + "false". Although I can change the case after getting the string, is there any other solution available? Could someone explain to me why it is happening?

Upvotes: 0

Views: 2000

Answers (3)

Luaan
Luaan

Reputation: 63722

Don't rely on the automatic string conversion. Instead, just write what you need:

(isTrue ? "true" : "false")

(Ideally, you'd have a helper method do that for you, or you'd use constants instead of string literals)

This stems from the fact that IL's bool has values True and False. The lowercase variants are C#'s keywords, and they are basically aliases for the "real" bool values.

You can see the same thing happening when you do e.g. string.GetType().Name - string is the C# alias for a type named System.String.

Upvotes: 1

samy
samy

Reputation: 14962

True and False are the normal string conversion of the boolean values in .net. What you are seeing (true and false) are the debugger values. Just use the ToLower method on the string converted boolean.

This method returns the constants "True" or "False". Note that XML is case-sensitive, and that the XML specification recognizes "true" and "false" as the valid set of Boolean values. If the String object returned by the ToString() method is to be written to an XML file, its String.ToLower method should be called first to convert it to lowercase.

Upvotes: 4

Selman Genç
Selman Genç

Reputation: 101681

Just convert your bool variable to string and use string.ToLower method:

 licenseID += " " + istrue.ToString().ToLower();

Upvotes: 4

Related Questions