What is Cast Operator () in C#?


Type conversion is converting one type of data to another type. Explicit conversions are done explicitly by users using the pre-defined functions and require a cast operator.

Let us see an example to cast double to int −

Example

using System;

namespace Demo {

   class Program {

      static void Main(string[] args) {
         double a = 4563.56;
         int x;

         x = (int)a;
         Console.WriteLine(x);
         Console.ReadKey();
      }
   }
}

To cast double to int, we perfomed explicit type casting −

x = (int)a;

Updated on: 21-Jun-2020

526 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements