Typecasting is the name given to transforming one type into another. There are two types of casting that can be performed on data types, implicit and explicit.


Implicit Typecast

Implicit casting is performed by the compiler when there is no possible loss of data. This is when a smaller data type is copied into a larger data type - e.g., converting a 16-bit short to a 32-bit integer value.
We could also say, implicit casting is used when casting from a basic type to a more general one. Therefore you use implicit typecasts when transforming a square into a rectangle or when transforming a circle into an ellipse. Because the later is the more general, the typecast can be done implicitly.
Here is a simple example to how implicit typecasting is done:
Int32 iNumber = 185; 
Double dNumber = 36.485d;
 
dNumber = iNumber; // implicit typecast; dNumber is now 185.0d
In this example the typecast can be done implicitly, because real numbers are a superset of integers. This means that Double is more general than Int32.
You can implement an implicit typecast in your classes using the implicit keyword. In our example we want to transform a square into a rectangle.
using System; 
using System.Drawing;
 
public class MySquare
{
// Remember: please do not make private variables public.
// Use accessors instead.
private Point pos;
private Int32 width;

#region Constructors, Methods etc.
// ...
#endregion
 
#region Methods
public Point Position
{
get { return this.pos; }
set { this.pos= value; }
}
 
public Int32 Width
{
get { return this.width; }
set { this.width = value; }
}
#endregion
}
 
public class MyRectangle
{
private Point pos;
private Int32 width, height;

#region Constructors, Methods etc.
// ...
#endregion
 
#region Implicit Type Conversion
public static implicit operator MyRectangle(MySquare Square)
{
MyRectangle rect = new MyRectangle();

rect.pos = Square.Position;
rect.width = Square.Width;
 
// Because MySquare has no heigth property we set a
// default value
rect.height = Square.Width;
return rect;
}
#endregion
}
Now we can do type conversion with ease:
MySquare square = new MySquare(...);
MyRectangle rectangle = square;

Explicit Typecast

Explicit casting requires the use of the cast operator (parentheses) when there is a possible loss of data - e.g., converting a 32-bit integer into an 8-bit byte as in the code below.
int fourBytes = 0x000000FE;
byte lowestByte = (byte)fourBytes;
Using the implicit example, we want to look at the case if there is a data loss on typecasting:
MyRectangle rectangle = new MyRectangle(...);
MySquare square = rectangle; // Error
Because a rectangle may have different width and height, it should not be possible to convert a rectangle into a square. However, we want to provide the users with the possibility to do so explicitly. We can solve this issue by defining how to handle this case.
A possibility to do so is checking if the width and height is equal. If so, the rectangle represents a square and we can convert gracefully. Otherwise, either a type cast exception has to get thrown.
public class MySquare
{
// ...
 
public explicit operator MySquare(MyRectangle Rectangle)
{
if(Rectangle.Width == Rectangle.Height)
{
this.Width = Rectangle.Width; // no information lost
this.pos = Rectangle.Position;
}
else
{
throw new TypeCastException("Rectangle is not square.");
}
}
}
Therefore, the explicit casting operation would look like this:
MyRectangle rectangle = new MyRectangle(...);
MySquare square = (MySquare)rectangle;
Alternatively we define how the information is transformed, which does not require any exceptions to be thrown.
public class MySquare
{
// ...
 
public explicit operator MySquare(MyRectangle Rectangle)
{
this.width = Rectangle.Width;
this.pos = Rectangle.Position;
// ignore the height of the rectangle
 
// my compiler complains that this operator requires the static keyword.
// After I added that, I had to change the code to eliminate the use of
// 'this' as in the previous example.
}
}