Data Types: Type conversion
C# is a strongly typed language which means you have to give a data type when you declare a variable. But there might occur a situation where you want to share data between different type of variables for this purpose you use type casting.
These are conversion about which you don’t have to worry. C# compiler deals them for you in type-safe manner. These are done when you care casting a smaller data type or larger data type.
Example
Output
Explicit Type Casting
These are conversion which can’t be done automatically. For these conversions you have to cast the data manually to tell the compiler that you are ok with this conversion because in these types of conversion some data might be lost. Converting larger data types to smaller data types are examples of explicit type casting.
Example
Type Casting
Converting one type of variable to another is called type casting. You can do this by casting one type of variable to another type of variable.Types of Type casting
There are two types of type casting.- Implicit type casting
- Explicit type casting
These are conversion about which you don’t have to worry. C# compiler deals them for you in type-safe manner. These are done when you care casting a smaller data type or larger data type.
Example
static void Main(string[] args) { byte b = 100; short s = 200; int i = 300; i = s; s = b; Console.WriteLine("The value of i is {0} and value of s is {1}.",i , s); Console.ReadLine(); }In above program you can see that you can assign value of short to int and value of byte to short because byte is 8 bits and short is 16 bits. So the 8 bit data can be stored in 16 bit space. Same goes for int which is 32 bits and can store short value which is 16 bit.
Output
Explicit Type Casting
These are conversion which can’t be done automatically. For these conversions you have to cast the data manually to tell the compiler that you are ok with this conversion because in these types of conversion some data might be lost. Converting larger data types to smaller data types are examples of explicit type casting.
Casting operator
When you want to cast a variable to your desired data type you have to put your desired data type in parenthesis and put it in front of the variable you are casting. E.g. byte b = (byte) i;Example
static void Main(string[] args) { byte b = 100; short s = 200; int i = 300; b = (byte)s; Console.WriteLine("The value of b is {0}.",b); b = (byte)i; Console.WriteLine("The value of b is {0}.", b); Console.ReadLine(); }Output
- Here you can see if you want to assign the value of short to byte you have to explicitly cast short to byte. For which you use casting operator.
- You have to use it because short is 16 bits and byte is 8 bits and you can’t put larger thing in smaller basket.
- To solve this problem we use casting operator and tell the compiler that we only want first 8 bits and you can leave all other part of that variable.
- In above example you can see that when the variable “s” is type casted into byte then the value of variable “b” is still 200 because that is stored in first 8 bits which is now allocated to variable “b”
- But when variable “i” is type casted into variable “b” then the value is reduced to 44. It happened because the first 8 bits of integer only holds 44 that is allocated to variable “b” and all other bits are just ignored.
Tags:
C-sharp

Muhammad Abid
Muhammad Abid is software engineer, a passionate programmer who have created many projects in C# and Java.
Get latest updates in your inbox
Popular Posts
How Yoga Nurtures Your Programming Skills
Understanding and analyzing basics of SEO
Creating SEO friendly URLs with PHP and SQL
Adobe AIR - Developing cross platform applications
Writing functions to insert, update, search, delete from SQL tables in PHP
Getting started with C#
Creating XML sitemap with PHP and SQL and submitting to search engines
Creating RSS feed in xml format with PHP and SQL
Creating thumbnails dynamically from images with PHP
Backup SQL database with PHP
Tags
CSV
Validation
Android
Java
Nashorn
PHP Mailer
cPanel
Facebook
Marketing
AdSense
Google
Technorati
Feathers
Starling
crop
PDF
Yoga
feedburner
rss feed
meta tags
SQL
backup database
Adobe Air
XML Sitemap
Syntaxhighlighter
Uploadify
Tinymce
screenshot
Text image
webmaster tools
Verifying website
web analysis
SEO
visual studio 2013
IDE