Casting and Converting - CSharp Language Basics

CSharp examples for Language Basics:Data Type Cast

Description

Casting and Converting

Demo Code

using System;// w  ww. j a va 2s.  co  m
using static System.Console;
using static System.Convert;
class Program
{
   static void Main(string[] args)
   {
      int a = 10;
      double b = a; // an int can be stored in a double
      WriteLine(b);
      long e = 10;
      int f = (int)e;
      WriteLine($"e is {e} and f is {f}");
      e = long.MaxValue;
      f = (int)e;
      WriteLine($"e is {e} and f is {f}");
      double g = 9.8;
      int h = ToInt32(g);
      WriteLine($"g is {g} and h is {h}");
      double i = 9.49;
      double j = 9.5;
      double k = 10.49;
      double l = 10.5;
      WriteLine($"i is {i}, ToInt(i) is {ToInt32(i)}");
      WriteLine($"j is {j}, ToInt(j) is {ToInt32(j)}");
      WriteLine($"k is {k}, ToInt(k) is {ToInt32(k)}");
      WriteLine($"l is {l}, ToInt(l) is {ToInt32(l)}");
   }
}

Result


Related Tutorials