Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Resolved '?' afterClasses and Classes in parentheses

Discussion in 'Scripting' started by el_core, Apr 3, 2021.

  1. el_core

    el_core

    Joined:
    Apr 3, 2021
    Posts:
    2
    Hi everyone,

    Been watching some Youtube videos about learning how to prog in Unity and C#. Got some questions and couldn't Google answers, because I hadn't found how to question it properly.

    Code (CSharp):
    1.         public static TextMesh CreateWorldText(string text, Transform parent = null, Vector3 localPosition = default(Vector3), int fontSize = 40, Color? color = null, TextAnchor textAnchor = TextAnchor.UpperLeft, TextAlignment textAlignment = TextAlignment.Left, int sortingOrder = sortingOrderDefault) {
    2.             if (color == null) color = Color.white;
    3.             return CreateWorldText(parent, text, localPosition, fontSize, (Color)color, textAnchor, textAlignment, sortingOrder);
    4.         }
    Could you explain, please:

    1) What is '?' operator after Color class? Is it regular conditional operator? Than why it doesn't have consequent : alternative? And why it equals something?

    I tried to find it out in the C# docs, but I've failed :) Maybe didn't look close enough.

    2) Why class Color in return type parameters is in parentheses? What does that mean? Removing parentheses gives a syntax error with color varuable. And removing the whole '(Color)' gives an error with 'parent' and 'text' (can't convert to string and transform accordingly)

    Sorry for grammar and noob questions :)

    Would be gratefull for any explanations or docs.
     
  2. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
    Kurt-Dekker, Bunny83 and el_core like this.
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,756
    This question mark operator is confusing because:

    1.
    ?
    is hard to google

    2.
    ?
    has at least THREE (3) completely-unrelated uses.

    For instance, here is the first use:

    If you declare a variable:

    Code (csharp):
    1. int foo;
    That's an integer. If you declare it this way:

    Code (csharp):
    1. int? foo;
    That is a nullable integer. Integers cannot normally be null, but an
    int?
    can be.

    That is the use in your example above. Since
    Color
    can't be null,
    Color?
    actually CAN be null.

    The second use is the ternary operator, a shortcut for "if else" that can be combined into a result:

    Code (csharp):
    1. result = condition ? ifTrue : ifFalse;
    That assigns either
    ifTrue
    to result or
    ifFalse
    to
    result
    , based on the boolean
    condition


    The third and newest use is the null coalesce operator, which is NOT good to use in Unity because of the
    UnityEngine.Object
    null test equality overload.

    Code (csharp):
    1. result = myObject?.dataField;
    This will conditionally check if
    myObject
    is null, and if it is non-null, it will dereference
    .dataField
    and assign it to
    result
    .

    Do NOT use the third version in Unity unless you know what you're doing. Instead, write the above as:

    Code (csharp):
    1. if (myObject != null)
    2. {
    3.   result = myObject.dataField;
    4. }
    That will always work.
     
    Last edited: Apr 4, 2021
  4. el_core

    el_core

    Joined:
    Apr 3, 2021
    Posts:
    2
    Thanks a lot both of you
     
    Kurt-Dekker likes this.