Browse Source

Add C# type test example (pattern matching) (#6732)

* Add C# type test example (pattern matching)
* Connect section to "is" section, put "advanced" note back
31 2 years ago
parent
commit
8a9729d900
1 changed files with 11 additions and 0 deletions
  1. 11 0
      tutorials/scripting/c_sharp/c_sharp_features.rst

+ 11 - 0
tutorials/scripting/c_sharp/c_sharp_features.rst

@@ -82,6 +82,17 @@ the result is always going to be ``false``.
         // This block can never happen.
     }
 
+You can also declare a new variable to conditionally store the result of the cast
+if the ``is`` operator returns ``true``.
+
+.. code-block:: csharp
+
+    if (GetNode("MySprite") is Sprite2D mySprite)
+    {
+        // The mySprite variable only exists inside this block, and it's never null.
+        mySprite.SetFrame(0);
+    }
+
 For more advanced type checking, you can look into `Pattern Matching <https://docs.microsoft.com/en-us/dotnet/csharp/pattern-matching>`_.