November 4, 2012

?: Operator return void

Question by Artur Keyan

I want to use the ?: operator intstead of if else

e.g.

var directory = new DirectoryInfo(path);
if (!directory.Exist())
{
    directory.Create();
}
else 
{
    // do nothing
}

I tried to use ?: like this:

var directory = new DirectoryInfo(path);
!directory.Exist() ? directory.Create() : void;

but it says “Invalid expression term ‘void’“, null also isn’t working.

Can anyone tell me a solution? Thanks.

Answer by Starx

Ternary operator are not designed to replace if/else They are to simplify assignment and declaration. It should be able to assign the result the statement to something like variable or function. There major use are in assignment or reading.

Something like

var status = !directory.Exist() ? directory.Create() : false;

The correct solution would be to stick to native if condition. The following is enough:

var directory = new DirectoryInfo(path);
if (!directory.Exist())
{
    directory.Create();
}
April 5, 2012

Is there a reason not to use <=> (null safe equals operator) in mysql instead of =?

Question by cellige

MySql provides a nice operator <=> that works with comparisons that could contain a null such as null <=> null or null <=> 5 etc giving back intuitive results as many programming languages. Where as the normal equals operator always just returns null, which catches many new MySql users such as myself awry.

Is there a reason MySql has both and not JUST the functionality in <=> ? Who really needs an operator that is effectively undefined with built in language types?

Answer by Starx

Is there a reason MySql has both and not JUST the functionality in <=>
?
The operators are completely different from each other.

<=> performs an equality comparison like the = operator, but returns 1 rather than NULL if both operands are NULL, and 0 rather than NULL if one operand is NULL.

Who really needs an operator that is effectively undefined with built
in language types?

This depends on case, just because you haven’t encountered such cases, does not mean nobody needs it.

...

Please fill the form - I will response as fast as I can!