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. Other Discussions
  3. The Weird and The Wonderful
  4. When variable names in C# get weird

When variable names in C# get weird

Scheduled Pinned Locked Moved The Weird and The Wonderful
csharpvisual-studiohelpquestionannouncement
12 Posts 8 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.
  • L Lost User

    Take a look at this code

        static int test()
        {
            int \\u0066\\u006F\\u006F;
            return \\u0066\\u006F\\u006F;
        }
    

    Did you know you could do that? I didn't - I knew you could use unicode identifiers (though I never see them in real code), but this is a whole other level of madness. Obviously that variable is being used but is uninitialized. Try to guess what the compile error will be. It's not "Use of unassigned local variable '\u0066\u006F\u006F'". At least it is consistent though, it seems that identifiers written in that style are considered to be identical to the decoded version, so you can mix it with simplify writing it down, and it shows up in intellisense in decoded form and so on. It gets better. int is a keyword, so it's blue and you can't use it as a name for a local variable except with the @-prefix. So, this works:

            int \\u0069\\u006E\\u0074 = 0;
            @int++;
    

    That second line can be typed with intellisense auto-completion! That clever bastard knows about the variable called "int" and knows it had better put the @-prefix in front too, just typing "i" calls it up in the pop-up menu. On the other hand, the tool tip for the "int" entry also shows "(local variable) int int", but this is an entry with the value type icon. (this was all in VS2010, it may have changed since then) In total, I'm not sure whether to feel impressed or disgusted, or a bit of both.

    Kornfeld Eliyahu PeterK Offline
    Kornfeld Eliyahu PeterK Offline
    Kornfeld Eliyahu Peter
    wrote on last edited by
    #3

    I'm considering the option to write such a variable name as a bug... and wonder how the rest of the code looks like...

    Skipper: We'll fix it. Alex: Fix it? How you gonna fix this? Skipper: Grit, spit and a whole lotta duct tape.

    "It never ceases to amaze me that a spacecraft launched in 1977 can be fixed remotely from Earth." ― Brian Cox

    1 Reply Last reply
    0
    • L Lost User

      Take a look at this code

          static int test()
          {
              int \\u0066\\u006F\\u006F;
              return \\u0066\\u006F\\u006F;
          }
      

      Did you know you could do that? I didn't - I knew you could use unicode identifiers (though I never see them in real code), but this is a whole other level of madness. Obviously that variable is being used but is uninitialized. Try to guess what the compile error will be. It's not "Use of unassigned local variable '\u0066\u006F\u006F'". At least it is consistent though, it seems that identifiers written in that style are considered to be identical to the decoded version, so you can mix it with simplify writing it down, and it shows up in intellisense in decoded form and so on. It gets better. int is a keyword, so it's blue and you can't use it as a name for a local variable except with the @-prefix. So, this works:

              int \\u0069\\u006E\\u0074 = 0;
              @int++;
      

      That second line can be typed with intellisense auto-completion! That clever bastard knows about the variable called "int" and knows it had better put the @-prefix in front too, just typing "i" calls it up in the pop-up menu. On the other hand, the tool tip for the "int" entry also shows "(local variable) int int", but this is an entry with the value type icon. (this was all in VS2010, it may have changed since then) In total, I'm not sure whether to feel impressed or disgusted, or a bit of both.

      OriginalGriffO Offline
      OriginalGriffO Offline
      OriginalGriff
      wrote on last edited by
      #4

      That makes sense, in an odd sort of way. And this works:

      static int test()
      {
      int \u0066\u006F\u006F;
      foo = 6;
      return \u0066\u006F\u006F;
      }

      No compiler errors, just as you'd expect. I'd guess it's there to allow Chinese, Katakana, Persian, ... variable names and just happens to work in English as well. But whoever found that out and used it at the coalface should be taken out and shot...

      Bad command or file name. Bad, bad command! Sit! Stay! Staaaay... AntiTwitter: @DalekDave is now a follower!

      "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
      "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

      L 1 Reply Last reply
      0
      • OriginalGriffO OriginalGriff

        That makes sense, in an odd sort of way. And this works:

        static int test()
        {
        int \u0066\u006F\u006F;
        foo = 6;
        return \u0066\u006F\u006F;
        }

        No compiler errors, just as you'd expect. I'd guess it's there to allow Chinese, Katakana, Persian, ... variable names and just happens to work in English as well. But whoever found that out and used it at the coalface should be taken out and shot...

        Bad command or file name. Bad, bad command! Sit! Stay! Staaaay... AntiTwitter: @DalekDave is now a follower!

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #5

        OriginalGriff wrote:

        I'd guess it's there to allow Chinese, Katakana, Persian, ... variable names and just happens to work in English as well.

        Maybe? I don't see much point though, since they can be used without this hexadecimal encoding step. For example, there's no problem here:

            static int test()
            {
                int 変数 = 1;
                int 整数 = 2;
                return 変数 + 整数;
            }
        
        OriginalGriffO 1 Reply Last reply
        0
        • L Lost User

          OriginalGriff wrote:

          I'd guess it's there to allow Chinese, Katakana, Persian, ... variable names and just happens to work in English as well.

          Maybe? I don't see much point though, since they can be used without this hexadecimal encoding step. For example, there's no problem here:

              static int test()
              {
                  int 変数 = 1;
                  int 整数 = 2;
                  return 変数 + 整数;
              }
          
          OriginalGriffO Offline
          OriginalGriffO Offline
          OriginalGriff
          wrote on last edited by
          #6

          Yes, but I couldn't type them - they all look too similar to me. So I'd need the Unicode values or copy'n'paste to modify that code.

          Bad command or file name. Bad, bad command! Sit! Stay! Staaaay... AntiTwitter: @DalekDave is now a follower!

          "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
          "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

          1 Reply Last reply
          0
          • L Lost User

            Take a look at this code

                static int test()
                {
                    int \\u0066\\u006F\\u006F;
                    return \\u0066\\u006F\\u006F;
                }
            

            Did you know you could do that? I didn't - I knew you could use unicode identifiers (though I never see them in real code), but this is a whole other level of madness. Obviously that variable is being used but is uninitialized. Try to guess what the compile error will be. It's not "Use of unassigned local variable '\u0066\u006F\u006F'". At least it is consistent though, it seems that identifiers written in that style are considered to be identical to the decoded version, so you can mix it with simplify writing it down, and it shows up in intellisense in decoded form and so on. It gets better. int is a keyword, so it's blue and you can't use it as a name for a local variable except with the @-prefix. So, this works:

                    int \\u0069\\u006E\\u0074 = 0;
                    @int++;
            

            That second line can be typed with intellisense auto-completion! That clever bastard knows about the variable called "int" and knows it had better put the @-prefix in front too, just typing "i" calls it up in the pop-up menu. On the other hand, the tool tip for the "int" entry also shows "(local variable) int int", but this is an entry with the value type icon. (this was all in VS2010, it may have changed since then) In total, I'm not sure whether to feel impressed or disgusted, or a bit of both.

            R Offline
            R Offline
            RugbyLeague
            wrote on last edited by
            #7

            sequences of underscores are also valid variable names

            1 Reply Last reply
            0
            • L Lost User

              Take a look at this code

                  static int test()
                  {
                      int \\u0066\\u006F\\u006F;
                      return \\u0066\\u006F\\u006F;
                  }
              

              Did you know you could do that? I didn't - I knew you could use unicode identifiers (though I never see them in real code), but this is a whole other level of madness. Obviously that variable is being used but is uninitialized. Try to guess what the compile error will be. It's not "Use of unassigned local variable '\u0066\u006F\u006F'". At least it is consistent though, it seems that identifiers written in that style are considered to be identical to the decoded version, so you can mix it with simplify writing it down, and it shows up in intellisense in decoded form and so on. It gets better. int is a keyword, so it's blue and you can't use it as a name for a local variable except with the @-prefix. So, this works:

                      int \\u0069\\u006E\\u0074 = 0;
                      @int++;
              

              That second line can be typed with intellisense auto-completion! That clever bastard knows about the variable called "int" and knows it had better put the @-prefix in front too, just typing "i" calls it up in the pop-up menu. On the other hand, the tool tip for the "int" entry also shows "(local variable) int int", but this is an entry with the value type icon. (this was all in VS2010, it may have changed since then) In total, I'm not sure whether to feel impressed or disgusted, or a bit of both.

              N Offline
              N Offline
              Nicholas Marty
              wrote on last edited by
              #8

              This looks like it has been written by a decompiler. At least I've seen dotPeek from JetBrains show me code like this a few times, but I don't remember the version. Maybe someone decompiled another library to extract one (or a few) methods out of it without actually modifying the copied code?

              1 Reply Last reply
              0
              • L Lost User

                Take a look at this code

                    static int test()
                    {
                        int \\u0066\\u006F\\u006F;
                        return \\u0066\\u006F\\u006F;
                    }
                

                Did you know you could do that? I didn't - I knew you could use unicode identifiers (though I never see them in real code), but this is a whole other level of madness. Obviously that variable is being used but is uninitialized. Try to guess what the compile error will be. It's not "Use of unassigned local variable '\u0066\u006F\u006F'". At least it is consistent though, it seems that identifiers written in that style are considered to be identical to the decoded version, so you can mix it with simplify writing it down, and it shows up in intellisense in decoded form and so on. It gets better. int is a keyword, so it's blue and you can't use it as a name for a local variable except with the @-prefix. So, this works:

                        int \\u0069\\u006E\\u0074 = 0;
                        @int++;
                

                That second line can be typed with intellisense auto-completion! That clever bastard knows about the variable called "int" and knows it had better put the @-prefix in front too, just typing "i" calls it up in the pop-up menu. On the other hand, the tool tip for the "int" entry also shows "(local variable) int int", but this is an entry with the value type icon. (this was all in VS2010, it may have changed since then) In total, I'm not sure whether to feel impressed or disgusted, or a bit of both.

                R Offline
                R Offline
                raddevus
                wrote on last edited by
                #9

                And I just thought of this...

                int \u0066\u006F\u006F = 34;
                Console.WriteLine(nameof( \u0066\u006F\u006F));

                // writes out foo Works in C# 6.0 and above

                1 Reply Last reply
                0
                • L Lost User

                  Take a look at this code

                      static int test()
                      {
                          int \\u0066\\u006F\\u006F;
                          return \\u0066\\u006F\\u006F;
                      }
                  

                  Did you know you could do that? I didn't - I knew you could use unicode identifiers (though I never see them in real code), but this is a whole other level of madness. Obviously that variable is being used but is uninitialized. Try to guess what the compile error will be. It's not "Use of unassigned local variable '\u0066\u006F\u006F'". At least it is consistent though, it seems that identifiers written in that style are considered to be identical to the decoded version, so you can mix it with simplify writing it down, and it shows up in intellisense in decoded form and so on. It gets better. int is a keyword, so it's blue and you can't use it as a name for a local variable except with the @-prefix. So, this works:

                          int \\u0069\\u006E\\u0074 = 0;
                          @int++;
                  

                  That second line can be typed with intellisense auto-completion! That clever bastard knows about the variable called "int" and knows it had better put the @-prefix in front too, just typing "i" calls it up in the pop-up menu. On the other hand, the tool tip for the "int" entry also shows "(local variable) int int", but this is an entry with the value type icon. (this was all in VS2010, it may have changed since then) In total, I'm not sure whether to feel impressed or disgusted, or a bit of both.

                  N Offline
                  N Offline
                  Nathan Minier
                  wrote on last edited by
                  #10

                  Could be worse. You'll be happy to know that I tried to assign a value to int \u263a and the IDE kvetched.

                  "There are three kinds of lies: lies, damned lies and statistics." - Benjamin Disraeli

                  Richard DeemingR 1 Reply Last reply
                  0
                  • N Nathan Minier

                    Could be worse. You'll be happy to know that I tried to assign a value to int \u263a and the IDE kvetched.

                    "There are three kinds of lies: lies, damned lies and statistics." - Benjamin Disraeli

                    Richard DeemingR Offline
                    Richard DeemingR Offline
                    Richard Deeming
                    wrote on last edited by
                    #11

                    You just need an extra 0:

                    int \u0263a = 42;


                    "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                    "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

                    N 1 Reply Last reply
                    0
                    • Richard DeemingR Richard Deeming

                      You just need an extra 0:

                      int \u0263a = 42;


                      "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                      N Offline
                      N Offline
                      Nathan Minier
                      wrote on last edited by
                      #12

                      Sure, but that's not an emoticon.

                      "There are three kinds of lies: lies, damned lies and statistics." - Benjamin Disraeli

                      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