top of page

C# Tutorial For Beginners: Casting Variables

Updated: Jan 18, 2022

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 convert 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 are some examples demonstrating how ‘implicit’ casting is done.

Explicit Casting

Explicit casting is done using a dedicated syntax that 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
bottom of page