| Start |
|---|
| Cast an object to a base type Given the following definitions : Casting an object to a base type example : |
| Checking compatibility without casting If you need to know whether a value's type extends or implements a given type, but you don't want to actually cast it as that type, you can use the |
| Explicit Casting If you know that a value is of a specific type, you can explicitly cast it to that type in order to use it in a context where that type is needed. If we tried passing If |
Safe Explicit Casting (as operator) If you aren't sure whether a value is of the type you think it is, you can safely cast it using the Note that |
| Implicit Casting A value will automatically be cast to the appropriate type if the compiler knows that it can always be converted to that type. In this example, we didn't need to use the typical explicit casting syntax because the compiler knows all |
| Explicit Numeric Conversions Explicit casting operators can be used to perform conversions of numeric types, even though they don't extend or implement one another. Note that in cases where the destination type has less precision than the original type, precision will be lost. For example, Also, numeric conversions rely on compile-time types, so they won't work if the numeric types have been "boxed" into objects. |
| Conversion Operators In C#, types can define custom Conversion Operators, which allow values to be converted to and from other types using either explicit or implicit casts. For example, consider a class that is meant to represent a JavaScript expression: If we wanted to create a JsExpression representing a comparison of two JavaScript values, we could do something like this: But we can add some explicit conversion operators to Or, we could change these operators to implicit to make the syntax much simpler. |
| LINQ Casting operations Suppose you have types like the following: LINQ allows you to create a projection that changes the compile-time generic type of an When When Due to the generic type of these methods, they cannot invoke Conversion Operators or perform numeric conversions. You can simply perform a cast inside a RemarksCasting is not the same as Converting. It is possible to convert the string value |
Read Article
https://develop.horje.com/learn/1434/reference