if (string) vs if (bool) in C#

Lunatiic

Is there a noticeable difference in performance/code quality if you use a string to compare at the start of an "if" instead of a boolean?

Example with string:

string isTrue = "true";
if (isTrue == "true"){
  // do something
}

Example with bool:

bool isTrue = true;
if (isTrue){
   //do something
}

I generally use a bool for these kind of comparisons but I've seen both variations online.

BWA

Based on code generated on tryroslyn

Code:

using System;
public class C {
    public void M() {
        string isStringTrue = "true";
        if (isStringTrue == "true")
        {
            // do something
        }              

        bool isBoolTrue = true;
        if (isBoolTrue)
        {
            //do something
        }
    }
}

Produce IL code:

.class private auto ansi '<Module>'
{
} // end of class <Module>

.class public auto ansi beforefieldinit C
    extends [mscorlib]System.Object
{
    // Methods
    .method public hidebysig 
        instance void M () cil managed 
    {
        // Method begins at RVA 0x2050
        // Code size 34 (0x22)
        .maxstack 2
        .locals init (
            [0] string,
            [1] bool,
            [2] bool,
            [3] bool
        )

        IL_0000: nop
        IL_0001: ldstr "true"
        IL_0006: stloc.0
        IL_0007: ldloc.0
        IL_0008: ldstr "true"
        IL_000d: call bool [mscorlib]System.String::op_Equality(string, string)
        IL_0012: stloc.2
        IL_0013: ldloc.2
        IL_0014: brfalse.s IL_0018
        IL_0016: nop
        IL_0017: nop
        IL_0018: ldc.i4.1
        IL_0019: stloc.1
        IL_001a: ldloc.1
        IL_001b: stloc.3
        IL_001c: ldloc.3
        IL_001d: brfalse.s IL_0021
        IL_001f: nop
        IL_0020: nop
        IL_0021: ret
    } // end of method C::M

    .method public hidebysig specialname rtspecialname 
        instance void .ctor () cil managed 
    {
        // Method begins at RVA 0x207e
        // Code size 8 (0x8)
        .maxstack 8

        IL_0000: ldarg.0
        IL_0001: call instance void [mscorlib]System.Object::.ctor()
        IL_0006: nop
        IL_0007: ret
    } // end of method C::.ctor

} // end of class C

As you can see compare strings needs call method, compare bools don't need this.

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Objective-C:BOOL vs bool

分類Dev

C++ Implicit conversion from bool to string

分類Dev

bool? compare with bool vs GetValueOrDefault vs ?? operator

分類Dev

Bitarray VS bool[]

分類Dev

BOOL vs. bool on iOS7

分類Dev

bool x =(A || B && C)

分類Dev

C ++ boolからstringへの暗黙的な変換

分類Dev

C++ string (const char[1]) to bool type conversion - reason/explanation and consequences

分類Dev

convert vector<bool> to decimal (as string)

分類Dev

AJAX and PHP returning bool and string

分類Dev

文字列ss = ""; vs文字列ss = string.empty; vs string = null; C#で

分類Dev

C++ concatenation; string + double; operator+ vs operator+=

分類Dev

C#: Different string encoding on attribute vs. constant

分類Dev

How to look content of dynamic string array in c++ in VS debugger

分類Dev

Get stack trace to string in MS VS C++

分類Dev

How to use bool in C#

分類Dev

Swift 4 Codable - Bool or String values

分類Dev

Convert string containing bits to vector<bool>

分類Dev

C#-boolからbool *に変換

分類Dev

Default advice for using C-style string literals vs. constructing unnamed std::string objects?

分類Dev

std :: string s1 {"Modern C ++"、3} vs std :: string s1 {str、3}

分類Dev

toString vs String in Swift

分類Dev

How to Type Cast null as Bool in C#?

分類Dev

BOOL値の比較Objective_C

分類Dev

「Int-> Bool」、「Int-> Bool-> Int」、「Int-> String-> Int-> Bool」のタイプ

分類Dev

C / C ++ `!a` vs` a == 0`

分類Dev

C ++ 11でautofoo = "bar" vs std :: stringを使用する

分類Dev

boolによるstd :: stringの初期化

分類Dev

Why implicit conversion of bool to string isn't an error?

Related 関連記事

ホットタグ

アーカイブ