Search Unity

C# Beginner Tips #1 - Your Friend the Implicit Bool

Discussion in 'Community Learning & Teaching' started by FWCorey, Jun 11, 2013.

  1. FWCorey

    FWCorey

    Joined:
    Feb 2, 2011
    Posts:
    42
    I've started a new series of tips for beginners to C# scripting in Unity just starting to make scripts beyond simple MonoBehaviours.
    The original post is on my Facebook page HERE
    and on my blog http://fwcorey.wordpress.com/

    Your Friend the Implicit Bool

    Have you ever created your own class in Unity not derived from ScriptableObject or MonoBehaviour and noticed you can't check for null like you're used to, as seen below?

    Code (csharp):
    1. if ( MyClass ) DoSomething();
    Some of you (probably most of you, or maybe I'm being cynical) will probably just want the code. Since I'm not a preachy type and laziness is the whole point of this shortcut in the first place, here is the method you need to add. Simply replace "$classname$" with the name of the class you are adding it to.

    Code (csharp):
    1. public static implicit operator bool($classname$ me)
    2. {
    3.     return me != null;
    4. }
    Now you may be asking why I used “$classname$” instead of something like “MyClass” or “SomeClass”. This brings us to the next tip, Code Snippets. If I get at least 1 comment asking about them I'll explain those in my next blog post. :)

    If you're still a novice C# coder you may have wondered why this is. The answer that gives you back this convenient shortcut to checking for not-equal-to null, is an implicit bool.

    According to MSDN, “an implicit keyword is used to declare an implicit user-defined type conversion operator”. Now to someone just learning to use a little C# for scripting in Unity this might as well be medieval poetry in a foreign languge. So lets break it down.

    Keywords are read by the compiler and tell it what limits to place on a type or variable. The most common ones you are probably already used to using are public and private. The static keyword in our method means that this method can be used at any time without needing to be used on a variable of this "type".

    Operators are what we use to compare two values or modify one. The most common ones we use are “<,>,== and !=” which all return (or in otherwords perform a conversion to bool) a boolean value, (true or false).

    Boolean is a “type”. Types are basically structs or classes. So a “user-defined type” is simply a class you have created yourself.

    Well you may have seen an error before, when you forgot to precede a command with (ClassName) saying something similar to "Cannot implicitly convert type foo to type bar. An explicit conversion exists...". What this means is the interpreter doesn't know how to convert the class from one type into an equivalent value in the other type.

    Now if you are doing a check in an if() statement the interpreter is expecting a boolean return value (true or false), normally we generate this with boolean operators, “<,>,,==,!=” etc... and check one value against another. Checking for null values though is something we have to do frequently and typying:
    Code (csharp):
    1.  if ( myclass != null )
    ...over and over again can get a bit tedious even though it's only an extra 8 characters.

    Now programmers by nature are at least a little lazy when typing (though we like to call it "obsessed with efficiency") because we have to do so much of it, so implicit operators like this can be a little seductive. In this particular instance, since the practice of adding this check is so common we can make it a habit with little concern. We should however use the explicit keyword in most other situations. I'll get into why in a future blog post.
     
    Last edited: Jun 17, 2013
  2. marcelo_augusto

    marcelo_augusto

    Joined:
    Nov 18, 2018
    Posts:
    12
    And over 10 years later I came to request my piece of information on snippets...