I suggest you ...

Boolean coercion in c#

Can't believe this isn't a request already, but probably my biggest annoyance.

I really should be able to do

if(someObj)
someObj.Something();

rather than having to do

if(!Object.ReferenceEquals(null, someObj))
someObj.Something();

It doesn't have to do full javascript-style coercion (though extensiblity would be swell), just a simple != null check would cover 99% of scenarios.

18 votes
Vote
Sign in
Check!
(thinking…)
Reset
or sign in with
  • facebook
  • google
    Password icon
    I agree to the terms of service
    Signed in as (Sign out)
    You have left! (?) (thinking…)
    George MauerGeorge Mauer shared this idea  ·   ·  Flag idea as inappropriate…  ·  Admin →

    7 comments

    Sign in
    Check!
    (thinking…)
    Reset
    or sign in with
    • facebook
    • google
      Password icon
      I agree to the terms of service
      Signed in as (Sign out)
      Submitting...
      • Victor ZakharovVictor Zakharov commented  ·   ·  Flag as inappropriate

        @GeorgeMauer: This feature is coming in Visual Studio 2015. If I got it correctly, you should be able to do someObj?.Something(). I am not sure though, could be that it only works for return types, i.e. functions/properties.

      • Péter JuhászPéter Juhász commented  ·   ·  Flag as inappropriate

        Script-style constructions have definitely no place in a strongly typed language like C#. The condition expression of the if statement must be a boolean and not an object.

      • Hadrien ThomasHadrien Thomas commented  ·   ·  Flag as inappropriate

        And what would do:

        if(obj && boolean) ?

        Javascript has so lazy notations and behaviour that it's the most bugged language in the world.
        Let's not take anything from it.

      • George MauerGeorge Mauer commented  ·   ·  Flag as inappropriate

        Right, sure, if you prefer to do != null to allow for operator overriding then sure, we can do that, but the point is to have the implicit conversion so you don't have to do the comparison explicitly

      Feedback and Knowledge Base