Bottom value Boolean.ToString ()

I deduced the value of a booleanin my view MVCand would like to have lowercase trueor false, and not the default trueor false.

I appreciate that I can just do this:

@this.Model.MyBool.ToString().ToLower()

or use some extension method, etc.

but it seems to hit the target:

@this.Model.MyBool

I read this post here: Why is the output of Boolean.ToString "True" and not "true" , but most of the answers seem pretty old.

Are there even more modern ways to do this?

+6
source share
3 answers

bool, @Mohamed. , ( ):

public static class Extensions
{
    public static string ToLowerString(this bool _bool)
    {
        return _bool.ToString().ToLower();
    }
}

, :

public static void Main()
{
    bool testBoolean = true;
    Console.WriteLine(testBoolean.ToLowerString());
}
+7

public string MyStringBool
{
    get { return MyBool ? "true" : "false" ; }
}
+8

, , true.ToString().ToLower() ( ):

Json.Encode(true);

, .ToString.ToLower().

Json.Encode(object value) , .

, , , :

var sw0 = Stopwatch.StartNew();
sw0.Stop();

var sw1 = Stopwatch.StartNew();
var t1 = System.Web.Helpers.Json.Encode(true);
var e1 = sw1.ElapsedMilliseconds; // returns 6-9

var sw2 = Stopwatch.StartNew();
var t2 = true.ToString().ToLower();
var e2 = sw2.ElapsedMilliseconds; // returns 0

.

+1

Source: https://habr.com/ru/post/1542003/


All Articles