Login Register
Code2night
  • Home
  • Guest Posts
  • Blog Archive
  • Tutorial
  • Languages
    • Angular
    • C
    • c#
    • C#
    • HTML/CSS
    • Java
    • JavaScript
    • Node.js
    • Python
    • React
    • Security
    • SQL Server
    • TypeScript
  • Post Blog
  • Tools
    • JSON Beautifier
    • HTML Beautifier
    • XML Beautifier
    • CSS Beautifier
    • JS Beautifier
    • PDF Editor
    • Word Counter
    • Base64 Encode/Decode
    • Diff Checker
    • JSON to CSV
    • Password Generator
    • SEO Analyzer
  1. Home
  2. Blogpost

Type Casting in C#

Date- Dec 09,2023

3222

csharp type casting

Type Casting in C#

When the variable of one data type is changed to another data type is known as the Type Casting. According to our needs, we can change the type of data.after the declaration of the variable, we cannot declare it again. The value of the variable cannot be assigned to another type of variable unless we implicitly change the type of the variable.

In C#, there are two types of casting

  • Implicit Casting
  • Explicit Casting

Implicit Casting :- These conversions are performed by C# in a type-safe manner. For example, are conversions from smaller to larger integral types and conversions from derived classes to base classes.

Example:- char -> int -> long -> float -> double


using System;

namespace MyApplication
{
  class Program
  {
    static void Main(string[] args)
    {
      int myInt = 4;
      double myDouble = myInt;  // Automatic casting: int to double

      Console.WriteLine(myInt);
      Console.WriteLine(myDouble);
    }
  }
}

OUTPUT

4
4

Explicit Casting :- These conversions are done explicitly by users using the pre-defined functions. Explicit conversions require a cast operator. There may be compilation error when types not compatible with each other. For example, assigning double value to int data type:


using System;

namespace MyApplication
{
  class Program
  {
    static void Main(string[] args)
    {
      double myDouble = 9.78;
      int myInt = myDouble;  

      Console.WriteLine(myDouble);
      Console.WriteLine(myInt);
    }
  }
}

OUPUT

error CS0266: Cannot implicitly convert type `double' to `int'. An explicit conversion exists (are you missing a cast?)
Compilation failed: 1 error(s), 0 warnings

Now, using Explicit casting must be done manually by placing the type in parentheses in front of the value:

Example:- double -> float -> long -> int -> char


using System;

namespace MyApplication
{
  class Program
  {
    static void Main(string[] args)
    {
      double myDouble = 5.78;
      int myInt = (int) myDouble;  // Manual casting: double to int

      Console.WriteLine(myDouble);
      Console.WriteLine(myInt);
    }
  }
}

OUPUT

5.78
5

S
Shubham Saini
Programming author at Code2Night — sharing tutorials on ASP.NET, C#, and more.
View all posts →

Related Articles

Mastering Async and Await in C#: A Comprehensive Guide
Mar 16, 2026
Introduction to C# Programming: Your First Steps in Software Development
Mar 08, 2026
Get random number in asp.net C#
Dec 23, 2023
Create JSON String in C#
May 31, 2023

Comments

Contents

More in C#

  • Zoom C# Wrapper Integration 12887 views
  • Convert HTML String To Image In C# 11444 views
  • The report definition is not valid or is not supported by th… 10773 views
  • Replacing Accent Characters with Alphabet Characters in CSha… 9730 views
  • Get IP address using c# 8591 views
View all C# posts →

Tags

AspNet C# programming AspNet MVC c programming AspNet Core C software development tutorial MVC memory management Paypal coding coding best practices data structures programming tutorial tutorials object oriented programming Slick Slider StripeNet
Free Download for Youtube Subscribers!

First click on Subscribe Now and then subscribe the channel and come back here.
Then Click on "Verify and Download" button for download link

Subscribe Now | 1760
Download
Support Us....!

Please Subscribe to support us

Thank you for Downloading....!

Please Subscribe to support us

Continue with Downloading
Be a Member
Join Us On Whatsapp
Code2Night

A community platform for sharing programming knowledge, tutorials, and blogs. Learn, write, and grow with developers worldwide.

Panipat, Haryana, India
info@code2night.com
Quick Links
  • Home
  • Blog Archive
  • Tutorials
  • About Us
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Guest Posts
  • SEO Analyzer
Free Dev Tools
  • JSON Beautifier
  • HTML Beautifier
  • CSS Beautifier
  • JS Beautifier
  • Password Generator
  • QR Code Generator
  • Hash Generator
  • Diff Checker
  • Base64 Encode/Decode
  • Word Counter
  • SEO Analyzer
By Language
  • Angular
  • C
  • c#
  • C#
  • HTML/CSS
  • Java
  • JavaScript
  • Node.js
  • Python
  • React
  • Security
  • SQL Server
  • TypeScript
© 2026 Code2Night. All Rights Reserved.
Made with for developers  |  Privacy  ·  Terms
Translate Page