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

* Add C# type test example (pattern matching)
* Connect section to "is" section, put "advanced" note back
This commit is contained in:
31
2023-02-05 07:22:07 -06:00
committed by GitHub
parent bec737cb75
commit 8a9729d900

View File

@@ -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>`_.