| @@ -1,16 +1,18 @@ | |||||
| # Basic Operations Questions | # Basic Operations Questions | ||||
| ## How should I safely check a type? | ## How should I safely check a type? | ||||
| > [!WARNING] | |||||
| > Direct casting (e.g. `(Type)type`) is **the least recommended** | |||||
| > way of casting, as it *can* throw an [InvalidCastException] | |||||
| > when the object isn't the desired type. | |||||
| > | |||||
| > Please refer to [this post] for more details. | |||||
| In Discord.NET, the idea of polymorphism is used throughout. You may | In Discord.NET, the idea of polymorphism is used throughout. You may | ||||
| need to cast the object as a certain type before you can perform any | need to cast the object as a certain type before you can perform any | ||||
| action. | action. | ||||
| There are several ways to cast, with direct casting | |||||
| `(Type)type` being **the least recommended**, as it *can* throw an | |||||
| [InvalidCastException] when the object isn't the desired type. | |||||
| Please refer to [this post] for more details. | |||||
| A good and safe casting example: | A good and safe casting example: | ||||
| [!code-csharp[Casting](samples/basics/cast.cs)] | [!code-csharp[Casting](samples/basics/cast.cs)] | ||||
| @@ -83,6 +85,8 @@ reactions. | |||||
| Unfortunately, not at the moment. See [#401](https://github.com/RogueException/Discord.Net/issues/401). | Unfortunately, not at the moment. See [#401](https://github.com/RogueException/Discord.Net/issues/401). | ||||
| [IChannel]: xref:Discord.IChannel | |||||
| [ICategoryChannel]: xref:Discord.ICategoryChannel | |||||
| [IGuildChannel]: xref:Discord.IGuildChannel | [IGuildChannel]: xref:Discord.IGuildChannel | ||||
| [ITextChannel]: xref:Discord.ITextChannel | [ITextChannel]: xref:Discord.ITextChannel | ||||
| [IGuild]: xref:Discord.IGuild | [IGuild]: xref:Discord.IGuild | ||||