By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,458 Members | 1,201 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,458 IT Pros & Developers. It's quick & easy.

Unboxing simple types

P: n/a
Hi,

I use C# 2.0 with the following:

object obj = ( int ) 1;

func( obj ); // Unboxing int to uint

void func( object obj )
{
#if true
uint v = ( uint ) obj; // Throws an invalid cast exception
#else
if( obj.GetType() == typeof( int ) ) // UGLY
{
uint v = ( uint )( int ) obj;
}
else
{
uint v = ( uint ) obj;
}
#endif
}

So I have the method which receives boxed numeric positive values, both
signed and unsigned. The method knows that the parameter is a numeric value
which should be convertible to uint. Is there an elegant method of casting
the parameter to uint without having to check the type of the parameter
first?

Thanks

Jun 6 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a
muriwai <mu*****@nospam.nospamwrote:

<snip>
So I have the method which receives boxed numeric positive values, both
signed and unsigned. The method knows that the parameter is a numeric value
which should be convertible to uint. Is there an elegant method of casting
the parameter to uint without having to check the type of the parameter
first?
Convert.ToUInt32() perhaps?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jun 6 '07 #2

P: n/a
Thanks!
"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:MP*********************@msnews.microsoft.com. ..
muriwai <mu*****@nospam.nospamwrote:

<snip>
>So I have the method which receives boxed numeric positive values, both
signed and unsigned. The method knows that the parameter is a numeric
value
which should be convertible to uint. Is there an elegant method of
casting
the parameter to uint without having to check the type of the parameter
first?

Convert.ToUInt32() perhaps?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Jun 6 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.