Jump to content
  • Advertisement
Sign in to follow this  
atoi

[.net] c# typecasting

This topic is 4700 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello everyone, I have finally come to a bit of a problem with c#, how do I typecast? I was using c++ for a while but I figured it would be wise to change with the times and move to c#. Thanks in Advance. Regards, Mike

Share this post


Link to post
Share on other sites
Advertisement
Alright so it seems there are two ways. just to be clear, (float)this.width / 2 * 3 would work?

Share this post


Link to post
Share on other sites
Yes, you can use C-style casts: (float)myInt; or the as keyword: myInt as float.

I can't remember exactly (check MSDN, it's your friend!) and don't have a reference handy, but casting one way throws exceptions for invalid casts and casting the other way returns a null for invalid casts. I believe the as keyword gives you a null on an invalid cast and is only applicable to classes/structs (not intrinsic types like int, float), but I may be wrong, so check MSDN...

Share this post


Link to post
Share on other sites
He's right:

Car c = (Vehicle)thingy;
Car c = thingy as object;


These are both legal. However, in case of an invalid cast, the first throws an exception while the second sets 'c' to null.

The latter can indeed only be applied to reference values, which are classes and structs.
That's why this:

float a = b as int;


is illegal.

Share this post


Link to post
Share on other sites
Quote:
Original post by atoi
Alright so it seems there are two ways. just to be clear, (float)this.width / 2 * 3 would work?


That will convert the Width to float before doing any other operation. Dividing or multiplying by 2.0f or 3.0f will accomplish the same result.

Toolmaker

Share this post


Link to post
Share on other sites
A simple cast throws an exception when it fails:

try
{
NewType castedVariable = (NewType)someVariable;
}
catch
{
// Error casting variable
}


The is keyword lets you check if it can be cast to the given type:

if(someVariable is NewType)
{
// It can be cast
}
else
{
// Nopers
}


I prefer the as operator. It returns null if it can't be cast, or the new casted object if it can:

NewType someVariable = oldVariable as NewType;
if(someVariable == null)
{
// Nopers
}
else
{
// Yay!
}


As mentioned earlier, they need to be objects as opposed to values or structs.

Share this post


Link to post
Share on other sites
I think I also read somewhere that 'as' is a bit faster than '()' casting. I suppose it makes sense since 'as' uses the IL instruction 'isinst' while '()' casting uses the 'castclass' instruction. But as everyone says, micro-optimisation is not good until you've profiled so it's just good practise really.

Happy New Year!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!