From 8a9729d900ff3fad8db055aca56fbc4f72775e89 Mon Sep 17 00:00:00 2001 From: 31 <331300+31@users.noreply.github.com> Date: Sun, 5 Feb 2023 07:22:07 -0600 Subject: [PATCH] Add C# type test example (pattern matching) (#6732) * Add C# type test example (pattern matching) * Connect section to "is" section, put "advanced" note back --- tutorials/scripting/c_sharp/c_sharp_features.rst | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/tutorials/scripting/c_sharp/c_sharp_features.rst b/tutorials/scripting/c_sharp/c_sharp_features.rst index b4d5a6b57..fc0aea6b9 100644 --- a/tutorials/scripting/c_sharp/c_sharp_features.rst +++ b/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 `_.