Unboxing simple types

  • Thread starter Thread starter muriwai
  • Start date Start date
M

muriwai

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
 
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?
 
Back
Top