Why does Boolean.ToString output True and not true
Only people from Microsoft can really answer that question. However, I'd like to offer some fun facts about it ;)
First, this is what it says in MSDN about the Boolean.ToString() method:
Return Value
Type: System.String
TrueString if the value of this
instance is true, or FalseString if
the value of this instance is false.Remarks
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.
Here comes the fun fact #1: it doesn't return TrueString or FalseString at all. It uses hardcoded literals "True" and "False". Wouldn't do you any good if it used the fields, because they're marked as readonly, so there's no changing them.
The alternative method, Boolean.ToString(IFormatProvider) is even funnier:
Remarks
The provider parameter is reserved. It does not participate in the execution of this method. This means that the Boolean.ToString(IFormatProvider) method, unlike most methods with a provider parameter, does not reflect culture-specific settings.
What's the solution? Depends on what exactly you're trying to do. Whatever it is, I bet it will require a hack ;)
Why delphi BoolToStr true is represented as -1
The source of these particular values is surely down to the 0
and -1
being the values used by the COM boolean type.
Certainly in older versions of the Delphi RTL this function was used when converting variants from one type to another, so I'd be reasonable confident that COM variant support was the reason behind this decision.
You can see the remnants of that original code today in VariantChangeSimpleIntoSimple
found in System.VarUtils
. When asked to convert varBoolean
to varOleStr
it does:
VarOleStrFromStr(Dest, BoolToStr(LSource.VBoolean))
Further reading:
- BOOL vs. VARIANT_BOOL vs. BOOLEAN vs. bool.
- Not Logical Is VBScript
Lower case Boolean.ToString() value
If you only want this for one bool variable you should use @Mohamed 's method. Else you can create an extension method (as you already said yourself):
public static class Extensions
{
public static string ToLowerString(this bool _bool)
{
return _bool.ToString().ToLower();
}
}
Then to use it:
public static void Main()
{
bool testBoolean = true;
Console.WriteLine(testBoolean.ToLowerString());
}
Why String.Concat returns 'True' instead of 'true' (the same with false)?
For the sample reason if you try to decompile String.Concat()
method in mscorlib.dll
you will get something like this
for (int index = 0; index < args.Length; ++index)
{
object obj = args[index];
values[index] = obj == null ? string.Empty : obj.ToString(); //which will call the `ToString()` of `boolean struct`
}
ToString()
method which is called by default by string.Concat
method it is like this
public override string ToString()
{
return !this ? "False" : "True";
}
Why does return true or false and break; doesnt work?
Just change your return type in SumMultiplier to bool (boolean type)
public static bool SumMultiplier(int[] arr) {
int sum = arr.Sum();
int doubleValue = sum*2;
int totalElements = arr.Count();
//Console.WriteLine("doble "+doubleValue);
//Console.WriteLine(totalElements);
//int j = totalElements;
for (int i=0; i<totalElements;i++){
//Console.WriteLine(arr[i]);
if(i+1<totalElements){
//Console.WriteLine("sds"+arr[i]*arr[i+1]);
for (int j=0; j<totalElements;j++){
//Console.WriteLine("numero iteracion "+i+" multiplica "+arr[i]+" por "+arr[j]);
int multRes = arr[j]*arr[i];
if(multRes>doubleValue){
return true;
break;
}
}
}
}
//return condition;
return false;
}
Related Topics
Automatically Set Appsettings.JSON for Dev and Release Environments in ASP.NET Core
Why We Have Both Jagged Array and Multidimensional Array
C# "Parameter Is Not Valid." Creating New Bitmap
Observeon and Subscribeon - Where the Work Is Being Done
Is Using a Mutex to Prevent Multiple Instances of the Same Program from Running Safe
C# Dictionary - One Key, Many Values
How to Use the Paint Event to Draw Shapes at Mouse Coordinates
How to Write to the Console in Colour in .Net
Test If a Property Is Available on a Dynamic Variable
Adding Your Own HTMLhelper in ASP.NET MVC 3
Differencebetween a Mutable and Immutable String in C#
How to Handle Key Press Event in Console Application
How to Do Appbar Docking (To Screen Edge, Like Winamp) in Wpf