Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. .NET (Core and Framework)
  4. MSIL String comparison question

MSIL String comparison question

Scheduled Pinned Locked Moved .NET (Core and Framework)
questioncsharpdotnet
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    Gordon Kushner
    wrote on last edited by
    #1

    Hey geniuses, it's me again. Google has failed me so I humbly come to you with a question about how .NET performs string comparisions. A tiny bit of background, I'm reseaching how the use of string constants is better than hardcoding string literals (beyond good coding practice). I wrote this code to check the various ways .NET handles strings.

    string s = "howdy";

    if (HELLO_W == "hello")
    {
    Console.WriteLine("condition 3");
    }

    And then checked the IDL with ildasm.exe

    IL_0033: ldstr "howdy"
    IL_0038: stloc.1
    IL_0039: ldc.i4.0
    IL_003a: stloc.2
    IL_003b: nop
    IL_003c: ldstr "condition 3"
    IL_0041: call void [mscorlib]System.Console::WriteLine(string)
    IL_0046: nop

    So where is the string comparision? When I compare two ints, I see the comparision command "ceq". As always, I appreciate your insights and am not afraid to kiss a little butt to get it. ;P

    K 1 Reply Last reply
    0
    • G Gordon Kushner

      Hey geniuses, it's me again. Google has failed me so I humbly come to you with a question about how .NET performs string comparisions. A tiny bit of background, I'm reseaching how the use of string constants is better than hardcoding string literals (beyond good coding practice). I wrote this code to check the various ways .NET handles strings.

      string s = "howdy";

      if (HELLO_W == "hello")
      {
      Console.WriteLine("condition 3");
      }

      And then checked the IDL with ildasm.exe

      IL_0033: ldstr "howdy"
      IL_0038: stloc.1
      IL_0039: ldc.i4.0
      IL_003a: stloc.2
      IL_003b: nop
      IL_003c: ldstr "condition 3"
      IL_0041: call void [mscorlib]System.Console::WriteLine(string)
      IL_0046: nop

      So where is the string comparision? When I compare two ints, I see the comparision command "ceq". As always, I appreciate your insights and am not afraid to kiss a little butt to get it. ;P

      K Offline
      K Offline
      Kubajzz
      wrote on last edited by
      #2

      If HELLO_W is a constant, then the result of the comparison is known at compile time and it will always be the same. I believe the compiler is smart enough to remove such unnecessary conditions... You might get different IL code depending on the compiler settings (Release/Debug).

      G 1 Reply Last reply
      0
      • K Kubajzz

        If HELLO_W is a constant, then the result of the comparison is known at compile time and it will always be the same. I believe the compiler is smart enough to remove such unnecessary conditions... You might get different IL code depending on the compiler settings (Release/Debug).

        G Offline
        G Offline
        Gordon Kushner
        wrote on last edited by
        #3

        Yep, that was it. Thank you. Now I'm getting this.

        IL_004a: call bool [mscorlib]System.String::op_Equality(string,string)

        Rookie mistake.

        K 1 Reply Last reply
        0
        • G Gordon Kushner

          Yep, that was it. Thank you. Now I'm getting this.

          IL_004a: call bool [mscorlib]System.String::op_Equality(string,string)

          Rookie mistake.

          K Offline
          K Offline
          Kubajzz
          wrote on last edited by
          #4

          You are welcome

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups