C# - Basics - Types - Explicit Conversion
Mart 4, 2012 by C# Tutorial
|
|
Article Information
This topic contains and tries to cover following subjects
- Explanation of explicit (manual) type conversion in C#
- Example of an explicit conversion in C#
Articles tries to provide answer to following questions and issues:
- What is explicit conversion in C#?
- When to use explicit conversions in C#?
Explanation of explicit (manual) type conversion in C#
In implicit conversion topic, we had mentioned about about when implicit conversions happen. To recall briefly:
byte x = 25;
int y = x; //OK...: implicit conversion happens automatically (byte is smaller than int, compiler converted byte variable to int without issue...)///
As it has been mentioned also in implicit conversions, from smaller types to bigger storage types, implicit conversion happens. However, there are cases that implicit conversion does not work. For example, when you try to assign a bigger storage type variable to smaller, implicit conversion fails.
Those are the conversions that can not be made implicitly. Idea behind is that the bigger types can cause data lose when they are being converted to smaller types. For example:
int y = 25;
byte x = y;//ERROR: Cannot implicitly convert type "int" to "byte". An explicit conversion exists (are you missing a cast?)///
Compiler indicates error for above assignment. Due to the reason of a possible data lose, that assignment code fails. Why data lose? if we recall, int keyword means int32, and it stores 32 bit data. Byte is 1 byte. 1 byte can hold values between 0-255. Int can hold values between 147483648 to 2147483647. Error is logical, since if we had a value like 265 as int variable, and try to assign it to byte variable, lets check what happens:
- In normal case we will get implicit conversion fails error:
int y1 = 254;
byte x1 = y1; //ERROR: implicit conversion fails for that assignment...///
To do that conversion explicitly, idea behind explicit conversion is to tell compiler that "we are sure of what we are doing (and we know we will lose data according to our assignment)". Lets do assignment explicitly. To do explicit conversion, we add the target type in parenthesis.
int y1 = 254;
byte x1 = (byte) y1; //OK...: we did an explicit conversion with saying convert to byte that it variable before assigning...///
However, consider that following scenario about that explicit conversion. If value of int variable, "y1" would be 256, what would that explicit conversion be? Int can store value 256, byte can store maximum 255. And we say compiler to do the conversion explicitly to store 256 in byte variable. Lets check:
int y2 = 256;
byte x2 = (byte) y2; //NO ERROR BUT DATA IS LOST: byte limit is 255 max///
In above example, if we explicitly tell that "y2" int type variable to convert byte for assigning into byte variable, you may see that "x2" value is 0. Our data within the variable is lost. In critical applications, this can cause problems.
Sometimes programmers may be aware of data lose when using explicit conversion. Main idea behind is, to let programmers make assignment "when they are sure of what they are doing".
Explicit conversion is also called "cast". In technical term, it is casting one type to another type with forcing compiler.
Example of an explicit conversion in C#
Following example demonstrates explicit conversion in C#. It is a console application. In example, we test followings:
- implicit conversion
- explicit conversion which does not cause data lose
- explicit conversion which cause data lose
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace NS_exampleApp
{
class CL_x
{
static void Main(string [] args)
{
//implicit conversion (from smaller to bigger)...///
byte x = 254;
int y = x;
//explicit conversion (from bigger to smaller)...///
//we are within limit of byte in our int variable...///
int y1 = 255;
byte x1 = (byte) y1; //OK...: explicit conversion without problem...///
//we are beyond the limit of byte...///
int y2 = 256;
byte x2 = (byte) y2; //NO ERROR BUT DATA LOSE: byte limit is 255 max///
Console.WriteLine("our int type y value (implicit conv.):\n" + y);
Console.WriteLine("our byte type x1 value (explicit conv. - no data lose:\n " + x1);
Console.WriteLine("our byte type x2 value (explicit conv - data lose:\n " + x2);
Console.ReadLine();
}
}
}
Output:
As we see in our example, explicit conversion caused data lose when:
- target is smaller type
- source type holds a value bigger than targets limit
It is essential point that compiler does not indicate compile errors when you use explicit conversion. Compiler assumes that programmer did that explicit forced conversion is done by programmer who knows what he is doing.
Data Layers
Area: | programming \ Languages \ csharp \ \ \ |
Ref: | http://msdn.microsoft.com/en-us/library/kca3w8x6%28v=VS.71%29.aspx |
Loc: | articles |
Tags: | csharp |
|