C# Exclamation Operator On Object
<p>In C# what do the following code fragments do? </p>

<p>1)</p>

<pre><code>if (!object)
{
  //code
}
</code></pre>

<p>2)</p>

<pre><code>if (object)
{
  //code
}
</code></pre>

<p>Where object is an instance of a class, and definately not <code>bool</code>.</p>

<p>1) In Java, trying the above code will make the compiler issue an error. Only boolean variables can be used as a Condition_Block. This works as expected.</p>

<p>2) In C++, <code>if (!object){/**/}</code> is used for null checks.</p>

<p>3) In C#, the compiler issues no error and happily compiles it. Google never mentions ! operator used to objects. It only gives search results on bool values. Furthermore to add insult to injury, it gives results of people talking about ? and ?? operators, which are operations that wont be available to unity developers for maybe 30-40 years. Only NET3.5 API is supported. If ! operator works as in C++, why do people need ? and ??.</p>

<p>Edit: Full code.</p>

<pre><code>using UnityEngine;
using System.Collections;

public class Foo : MonoBehaviour
{
    void Start ()
    {
        Foo a = new Foo();
        if (a)  Debug.Log("a");
        if (!a) Debug.Log("b");
    }
 }
</code></pre>

<p>it prints "b" on execution.</p>