Start a new topic

Language semantic

Some doubt about C# translation semantic: running on your try web page code like this:

   ...

   object a = false;

   object b = "0";


   if( a == b )

       System.Console.WriteLine( "Should I be here?" );


the output is "Should I be here?". Am I missing something?

Thanks


This is the nature of == operator in JavaScript. In JavaScript the following is true:

   

false == 0
0 == "0"
"0" == false

You can and should use object.ReferenceEquals(a, b) in such cases, that translates to === in JavaScript.

That's right, but the snippet is C# code, which won't print "Should I be here?" if executed in any .NET environment. The question is, should I write C# (syntax) using JS semantic (weird)? If so, any existing C# code cannot be used "as is", but must be pre-processed (manually?), changing  cases like object references equality (and others, I suppose). Why not just preserve the semantic or, if not possible' reject the code with an error?

Login or Signup to post a comment