An example of safe casting.
int myInt = 123; long myLong = myInt;
As you can see we do nothing different from normal variable assignment, this is because whenever we use a safe cast(implicit) the IDE will automatically cast it for us, thus the reason we see nothing different from normal code.
How is this a "safe" cast? Because a "long" variable can hold any value that an "int" variable can hold, meaning no matter what the int's value is, its safe to cast without chance for data loss.
Let's move onto explicit casting. This type of casting ignores type safety and will convert the value to the new type even if that means losing data.
For example look at this code:
double myDouble = 3.0; int myInt = myDouble;This code will generate an error because you cannot safely cast the double into and int without data loss, int will only hold the whole number(3) and will lose the fractional part(.0). We are a tad bit smarter then a computer though and we can see that losing .0 wont affect our outcome so we still want to create a int from the double's value. To do this we must manually cast the double value into an int.
Typical casting syntax looks like this:
<variable> = (castType)<secondVariable>;
So to cast our double into an int we can set the castType to int, like so.
double myDouble = 3.0; int myInt = (int)myDouble;There are two important things to note:
1. Whenever you use an explicit cast you must understand there is a chance for data loss.
2. The double did not become an int or vise versus. We just simply assigned an int from the doubles value.
Now that you understand how casting works we can learn a few other ways to cast something.
The first way is the "as" cast, which uses the as keyword to initiate the cast.
string myString = "CodeCall"; [COLOR=#006400]//Create a string.[/COLOR]
object myObj = myString; [COLOR=#006400]//Create an object from the string.[/COLOR]
string myString2 = myObj as string;[COLOR=#006400] //Cast myObj back to a string using the "as" keyword.[/COLOR]
if (myString2 == "CodeCall")[COLOR=#006400] //Check is our newly casted string is equal to our original string.[/COLOR]
{
Console.WriteLine("True");[COLOR=#006400]//Output "True".[/COLOR]
}
As you can see I didnt use our previous example of the double and int. This is because we cannot cast with the as keyword if the variable cannot have a null value. Int, doubles, ect cannot have null values, they must be assigned.The "is" operator creates an expression, returning a true or false value. This works great for an if-statement
int myInt = 3;
object myObj = myInt;[COLOR=#006400]//Create object from int.[/COLOR]
if (myObj is int)[COLOR=#006400] // Test if the object is int[/COLOR]
{
Console.WriteLine("True");
}
if (myObj is object)[COLOR=#006400]//Test if the object is also a object.[/COLOR]
{
Console.WriteLine("True");
}
The output will be:
casting-output.png 12.25K
42 downloadsSorry if the last bit is a little confusing but I'm in a big rush. I'll try and get back to it after this weekend. :closedeyes:
Thanks for reading, any comments, questions, or +rep welcome.
~ Committed.
Edited by CommittedC0der, 26 August 2011 - 12:53 PM.


Sign In
Create Account


Back to top









