mirror of
https://github.com/godotengine/godot-docs.git
synced 2026-01-03 05:48:42 +03:00
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:
@@ -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>`_.
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user