C Sharp Programming/Casting

Casting
Casting is a way to convert values from one type to another. Mainly, two types of casting exist, Implicit casting and Explicit casting. Boxing occurs when a value type is cast to an object, or reference type.

Implicit casting
Implicit casting is the way values are cast automatically by the compiler. This happens if and only if no loss of information occurs. Examples can be seen when converting data from smaller integral types to larger types, or derived types to the base type.

Also, this example could work.

Explicit casting
This type of casting has to be specified by the developer, as a loss of data is possible.

Best practices
For reference types, direct casting is discouraged unless the object is known to never throw an exception. A common best practice is to use the as keyword.

'''!Important: Using "as" keyword is the best practice only in case you are doing null check after the cast and then handle exception situation. In other cases better use explicit cast then debug a NullReferenceException'''