mardi 1 novembre 2016

C# Exclamation Operator On Object

In c# what do the following code fragments do?

1)

if (!object)
{
  //code
}

2)

if (object)
{
  //code
}

Where object is an instance of a class, and definately not bool.

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.

2) In c++, if(!object){...} is used for null checks.

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 the 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 used. If !exclamation operator works as in c++, why do people need ? and ??.

Aucun commentaire:

Enregistrer un commentaire