Implicit Typecas:
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. 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)
{
this.pos = Square.Position;
this.width = Square.Width;
// Because MySquare has no heigth property we set a
// default value
this.height = Square.Height;
}
#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's not possible to convert the rectangle into a square. 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.");
}
}
}
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
}
}
1 comments:
Thanks. Very Informative.
Post a Comment