cast
An explicit conversion written (Type)value that tells the compiler to treat a value as another type, and throws at run time if it is not.
In more detail
If the value is not actually of the target type, the cast throws InvalidCastException at run time. Generics and type patterns remove most casts, moving the failure from run time to compile time.
Related terms
- generics — Types that take other types as parameters, so one definition works for many kinds of value.
- type pattern — A pattern that tests a value's type and, if it matches, gives the converted value a name, as in if (boxed is int n).
Taught in
- Collections, Generics, and LINQ — Modern .NET Core, C# & Enterprise Microservices
- Records, Pattern Matching, and Modern C# — Modern .NET Core, C# & Enterprise Microservices