Casting in C#

Questpond
Posted by in C# category on for Intermediate level | Points: 250 | Views : 1120 red flag

In this article we will try to understand What is Conversion? What is Casting? When it should be used?
Recommendation
Read Event accessors and Lock statements in C# before this article.


Introduction

Let’s Understand Conversion

Implicit Conversion

Explicit Conversion without Casting

Explicit Conversion with Casting

Conclusion

Introduction

There are scenarios where we might come with the need of converting value from one datatype to another datatype. During this conversion we might lose some information. In this article lets us try to understand What is Conversion? What is Casting? When it should be used?

So in this article I will try to attempt to answer these questions.

Let’s Understand Conversion

If you understand the below two types of conversion you will never have problem around this concept.

There are two types of conversion : Implicit Conversion and Explicit Conversion.

  • Implicit Conversion do not requires Casting.
  • Implicit Conversion do not alters the information.
  • Explicit Conversion requires Casting.
  • Explicit Conversion lead to loss of information.

In the further article we will try to understand the above statements.

Implicit Conversion

Let us take the simple code given below.

staticvoid Main(string[] args)
{
   int i=10; // Declaration and Initialisation of Integer datatype i.
   float f; // Declaration of Float datatype f.
   f = i; // Assigning the value of i to f.
}

When we compile this program the compiler doesn’t gives any error and it builds successfully.

Here in the above case we are assigning a smaller datatype to larger datatype so we do not have to use Casting here. And the most important thing there is no loss of information.

Explicit Conversion without Casting

Let us take the simple code given below.

staticvoid Main(string[] args)
{
   int i; // Declaration of Integer datatype i.
   float f=100.56; // Declaration and Initialisation of Float datatype f.
   i = f; // Assigning the value of f to i.
}

When we compile the above program the compiler gives an error : “Cannot implicitly convert type double to int. An explicit conversion exists (are you missing a cast?) ” This error shows that we need Casting here.

So Casting is required when information might be lost in the conversion and when we want to store a larger datatype to a smaller datatype.

Explicit Conversion with Casting

Let’s correct the above code with cast operator.

staticvoid Main(string[] args)
{
   int i;
   float f=10.56;
   i = (int)f; // Using a cast operator.
}

When we compile this program the compiler doesn’t gives any error and it is build successfully. And its Output is : 10

The value in variable ‘f’ was ‘10.56’ and after conversion the value is changed to ‘10’. Hence there is loss of information in Casting. This happens because of the cast operator ‘(int)f’, it changes the datatype of ‘f’ to integer which was float and then assign that value to the integer variable removing the decimals .

Conclusion

Now casting though a simple concept can cause havoc and lead to defects which are unseen. For example let’s say you have a financial application where you are casting 10.50 to 10 and if you have lot of transaction just think how much loss your company would make.

But now take a situation of an application which measures temperature, for human 10 degrees and 10.1 degree does not matter too much.

So be very sure that the casting in your application not injecting unwanted and unseen defects which are later detected after a long time, but till them the damage is done.

Do watch the below Dotnetfunda youtube video which explains Casting in C# in a more detailed manner.

Recommendation
Read Boxing & Unboxing in C# after this article.
Page copy protected against web site content infringement by Copyscape

Login to vote for this post.

Comments or Responses

Login to post response

Comment using Facebook(Author doesn't get notification)