Lesson #3 - Casting Variables

Updated: Dec 13, 2020

A variable Casting process is when you assign a value of one data type to another type. In c#, there are two types of data casting:

  1. Implicit Casting – The cast operation is done automatically when we converting a smaller type to a larger type (int ->double, long->float, etc.).

  2. Explicit Casting – The cast operation is done manually in any case of converting a larger type to a smaller size type (long -> int, double ->float, etc.).

Implicit Casting

Let’s start with the simple part first, below some examples that will demonstrate how ‘implicit’ casting is done.

Explicit Casting

Explicit casting is done using a dedicated syntax which tells the compiler to approve the casting process although it may resolve data loss.

Type Conversion Methods

In addition to the examples above, we can also use built-in methods for converting data types as follow:

8 views0 comments