-2

Today I've come across an interesting bit of C# code in a Unity project:

MyScript ms = new MyScript(); //MyScript derives from MonoBehaviour
ms = null;
if(!ms) { Debug.Log("ms = "+(ms==null)); }

It does seem to behave the same as:

if(ms == null) { /*Do Stuff*/ }

But is it really the same thing? I haven't found any documentation about this anywhere yet. Is there a reason to not use the shorter version or prefer one over the other?

Neph
  • 1,823
  • 2
  • 31
  • 69
  • 6
    if(!myobject) doesn't compile, except that myobject is bool or you define a operator ! for your class – Antoine V Aug 30 '18 at 13:36
  • 1
    What is the type of `myobject`? – ColinM Aug 30 '18 at 13:37
  • I added an example, which does compile, even though it's not a bool. Because of this my question is not a duplicate. – Neph Aug 30 '18 at 13:42
  • You could do that if the class provided an implicit operator to `bool` but i discourage from using this kind of magic. `public static implicit operator bool(MyClass obj){ return obj != null; }` – Tim Schmelter Aug 30 '18 at 13:43
  • 2
    @TimSchmelter or the `true` + `false` operators; i.e. `public static bool operator true(Class1 x) => x != null;` and `public static bool operator false(Class1 x) => x == null;` – Marc Gravell Aug 30 '18 at 13:44
  • @Joel's answer and related comments fully answers your question. The type which myobject is an instance of is bool, implements an implicit cast to bool, or implements true/false operators. That's it. If you don't believe that, create a new class which doesn't implement these features, create an instance of it, and try the same test - it won't compile. – ProgrammingLlama Aug 30 '18 at 13:44
  • Whoever downvoted the question, even though it's not a duplicate, please explain why. – Neph Aug 31 '18 at 08:15
  • @Neph The question was downvoted because you did not provide enough information for a correct answer. I know you edited it later, but the downvotes were already given and most people just don't come back – Camilo Terevinto Sep 02 '18 at 16:18
  • @CamiloTerevinto How do you know if you've provided enough info beforehand? If your question is too long nobody reads it, if it's too short you get downvoted... The problem here is that I didn't even know that you can only do it with Unity's classes until someone said that C# usually doesn't work that way, yet it still got downvoted and even marked as a duplicate of something unrelated. – Neph Sep 03 '18 at 09:27
  • It's always better to provide as much info as possible and let people remove what's not relevant. But you don't need Unity, it's just that Unity uses that approach that's not normal in any other library/framework that I've worked with – Camilo Terevinto Sep 03 '18 at 11:23

1 Answers1

6

No. You can do that in Javascript, but C# doesn't work like that unless myobject is actually a boolean.


Based on this comment:

myobject ... derives from MonoBehaviour.

and this excerpt from the MonoBehavior docs:

Operators

bool           Does the object exist?  
operator !=    Compares if two objects refer to a different object.  

It looks like you can do this for your variable, becuase it is implicitly convertible to bool. But it's not generally okay for C#.

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
  • 6
    ... or an implicit operator is defined – Camilo Terevinto Aug 30 '18 at 13:37
  • 2
    @CamiloTerevinto or a `true`/`false` operator is defined, which is **different** to an implicit conversion operator – Marc Gravell Aug 30 '18 at 13:41
  • @MarcGravell Huh, this is the first time I see you can define a custom `true`/`false` operator... – Camilo Terevinto Aug 30 '18 at 13:45
  • `myobject` isn't a bool, it derives from [MonoBehaviour](https://docs.unity3d.com/ScriptReference/MonoBehaviour.html). – Neph Aug 30 '18 at 13:46
  • 1
    Reading the docs, the `MonoBehavior` type provides operator overloads for `!` and an implicit `bool` conversion. So this can work for for that object, but it's not generally okay in C#. – Joel Coehoorn Aug 30 '18 at 13:48
  • 1
    @CamiloTerevinto it is used *so amazingly rarely* that: I'm not surprised that folks aren't familiar with it – Marc Gravell Aug 30 '18 at 13:50
  • So it's another part of Unity "magic", thanks for the clarification! `but in C# those are not the same` - does it actually do something in "normal" C# or does it simply not compile? – Neph Aug 30 '18 at 13:57
  • 1
    It does not compile. – Joel Coehoorn Aug 30 '18 at 13:59
  • 2
    @Neph It's not magic, it's just the people that developed Unity loved JavaScript too much. Just look at the JavaScript-like spacing by default in scripts... – Camilo Terevinto Aug 30 '18 at 14:04
  • @CamiloTerevinto Call it a nice addition then. ;) As for the spacing: Visual Studio lets you change where you want spaces to be added automatically. – Neph Aug 30 '18 at 14:12