0% found this document useful (0 votes)
33 views5 pages

Type Casting

The document discusses type casting in C#, which is converting values from one data type to another. It explains implicit casting, which automatically converts smaller types to larger ones, and explicit casting, which requires manually placing the new type in parentheses. It provides examples of both and also describes built-in conversion methods.

Uploaded by

subashreenata
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views5 pages

Type Casting

The document discusses type casting in C#, which is converting values from one data type to another. It explains implicit casting, which automatically converts smaller types to larger ones, and explicit casting, which requires manually placing the new type in parentheses. It provides examples of both and also describes built-in conversion methods.

Uploaded by

subashreenata
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Search field


Log inSign Up Get Certified Spaces Set Goal
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C+
+ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PAND
AS NODEJS R TYPESCRIPT ANGULAR GIT POSTGRESQL MONGODB ASP AI G
O KOTLIN SASS VUE DSA GEN AI SCIPY AWS CYBERSECURITY DATA
SCIENCE

C# Tutorial
C# HOMEC# IntroC# Get StartedC# SyntaxC# OutputC# CommentsC# VariablesC# Data
TypesC# Type CastingC# User InputC# OperatorsC# MathC# StringsC# BooleansC#
If...ElseC# SwitchC# While LoopC# For LoopC# Break/ContinueC# Arrays

C# Methods
C# MethodsC# Method ParametersC# Method Overloading

C# Classes
C# OOPC# Classes/ObjectsC# Class MembersC# ConstructorsC# Access ModifiersC#
PropertiesC# InheritanceC# PolymorphismC# AbstractionC# InterfaceC# EnumsC#
FilesC# Exceptions

C# How To
Add Two Numbers

C# Examples
C# ExamplesC# CompilerC# ExercisesC# QuizC# ServerC# Certificate

C# Type Casting
❮ PreviousNext ❯

C# Type Casting
Type casting is when you assign a value of one data type to another type.

In C#, there are two types of casting:


 Implicit Casting (automatically) -
converting a smaller type to a larger
type size
char -> int -> long -> float -> double

 Explicit Casting (manually) -


converting a larger type to a smaller
size type
double -> float -> long -> int -> char

Implicit Casting
Implicit casting is done automatically when passing a smaller size type to a
larger size type:

ExampleGet your own C# Server


int myInt = 9;

double myDouble = myInt; // Automatic casting: int to double

Console.WriteLine(myInt); // Outputs 9

Console.WriteLine(myDouble); // Outputs 9

Try it Yourself »

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

Example
double myDouble = 9.78;

int myInt = (int) myDouble; // Manual casting: double to int


Console.WriteLine(myDouble); // Outputs 9.78

Console.WriteLine(myInt); // Outputs 9

Try it Yourself »

Type Conversion Methods


It is also possible to convert data types explicitly by using built-in methods,
such
as Convert.ToBoolean, Convert.ToDouble, Convert.ToString, Convert.ToInt32 (int)
and Convert.ToInt64 (long):

Example
int myInt = 10;

double myDouble = 5.25;

bool myBool = true;

Console.WriteLine(Convert.ToString(myInt)); // convert int to string

Console.WriteLine(Convert.ToDouble(myInt)); // convert int to double

Console.WriteLine(Convert.ToInt32(myDouble)); // convert double to int

Console.WriteLine(Convert.ToString(myBool)); // convert bool to


string

Try it Yourself »

Why Conversion?
Many times, there's no need for type conversion. But sometimes you have
to. Take a look at the next chapter, when working with user input, to see an
example of this.

❮ PreviousNext ❯

W3schools Pathfinder
Track your progress - it's free!
Log inSign Up

COLOR PICKER

SPACES
UPGRADE
AD-FREE
NEWSLETTER
GET CERTIFIED
CONTACT US

Top Tutorials

HTML Tutorial
CSS Tutorial
JavaScript Tutorial
How To Tutorial
SQL Tutorial
Python Tutorial
W3.CSS Tutorial
Bootstrap Tutorial
PHP Tutorial
Java Tutorial
C++ Tutorial
jQuery Tutorial

Top References
HTML Reference
CSS Reference
JavaScript Reference
SQL Reference
Python Reference
W3.CSS Reference
Bootstrap Reference
PHP Reference
HTML Colors
Java Reference
Angular Reference
jQuery Reference

Top Examples

HTML Examples
CSS Examples
JavaScript Examples
How To Examples
SQL Examples
Python Examples
W3.CSS Examples
Bootstrap Examples
PHP Examples
Java Examples
XML Examples
jQuery Examples

Get Certified

HTML Certificate
CSS Certificate
JavaScript Certificate
Front End Certificate
SQL Certificate
Python Certificate
PHP Certificate
jQuery Certificate
Java Certificate
C++ Certificate
C# Certificate
XML Certificate
 FORUM ABOUT CLASSROOM
W3Schools is optimized for learning and training. Examples might be simplified to improve reading
and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we
cannot warrant full correctness of all content. While using W3Schools, you agree to have read and
accepted our terms of use, cookie and privacy policy.

Copyright 1999-2024 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.

You might also like