0% found this document useful (0 votes)
8 views1 page

Implicit and Explicit Casting

The document explains the concepts of implicit and explicit casting in programming, particularly in C#. Implicit casting allows for automatic conversion from a smaller to a larger data type without special methods, while explicit casting requires a deliberate conversion due to potential data loss when moving from a larger to a smaller data type. Examples illustrate both casting methods, highlighting their respective risks and ease of use.
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)
8 views1 page

Implicit and Explicit Casting

The document explains the concepts of implicit and explicit casting in programming, particularly in C#. Implicit casting allows for automatic conversion from a smaller to a larger data type without special methods, while explicit casting requires a deliberate conversion due to potential data loss when moving from a larger to a smaller data type. Examples illustrate both casting methods, highlighting their respective risks and ease of use.
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/ 1

Implicit and Explicit Casting

When working with data types,sometimes we have to convert between data types.This can be lead to
unexpected errors sometimes.To avoid that,languages like C# has casting.

Implicit Casting

Imagine you are trying to put water in a small glass to a larger glass with more capacity.There is no
doubt that this is possible without any issue.It is safe to do that.
Similary,what was in a variable of a smaller data type can be assigned to a larger capacity data type
without any problem.You don’t need any special statement or methods for this.
(But remember when it comes to data types.is it not just the capacity we have to consider.We have to
consider the accessibility as well)

Ex: long  int  short  byte is possible with any problem


int x=20;
long y=x; //does not need any special method to cast.This is implicit casting.

Explicit Casting

Imagine you are trying to empty a larger glass of milk to a smaller glass of milk.Sometimes you may be
able to do it without spilling.But if the larger glass was full to it’s capacity,definitely milk will
overflow.When you are trying to put a value in a larger data type to a smaller capacity data type,you will
experience the same.
To avoid this,in C#.NET (and in many other languages) you have the explicit casting option .
It is pretty much like you are telling the program that you need to deliberately do this and you know the
possible risk of the conversion.

Ex: int x=56;


short y=Convert.ToShort(x);

You might also like