When variable names in C# get weird
-
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.
-
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.
I added the static method test() to a LINQPad script and it showed a squiggly line on the return statement. When I floated over it the error message is: CS0165 Use of unassiged local variable 'foo' That makes sense. I set the value to 34 on the first line and it works. Wow. Never knew. :cool:
-
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.
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.
-
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.
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!
-
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!
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 変数 + 整数; }
-
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 変数 + 整数; }
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!
-
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.
sequences of underscores are also valid variable names
-
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.
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?
-
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.
-
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.
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
-
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
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
-
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
Sure, but that's not an emoticon.
"There are three kinds of lies: lies, damned lies and statistics." - Benjamin Disraeli