Typecasting is a conversion of an expression to a given type, provided that the types are derived from each other, or a suitable conversion method exists. For example, an integer value can be cast to a double, and vice versa:
int i = 999;
double d = 123.456;
i = (int) d;
d = i;
Or
private void myButton_Click(object sender, EventArgs e)
{
if (sender is Button)
{
Button b = (Button) sender;
}
}
Or
foreach (Control c in Controls)
{
if (c is TextBox)
{
TextBox tb = (TextBox) c;
}
}
You cannot use casting to change between types where there is no defined conversion or relationship.
Boxing and unboxing are the process of "containing" a Value type (such as a integer, or struct) within a Reference type transparently so that it can be used as a reference. This is a bit complicated to explain, so have a look here:
Using struct and class - what's that all about?[
^] - there is a section on Boxing and what / why it is neccessary.