473,396 Members | 1,982 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Unboxing simple types

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
2 1318
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

43
by: Mountain Bikn' Guy | last post by:
I have a situation where an app writes data of various types (primitives and objects) into a single dimensional array of objects. (This array eventually becomes a row in a data table, but that's...
24
by: ALI-R | last post by:
Hi All, First of all I think this is gonna be one of those threads :-) since I have bunch of questions which make this very controversial:-0) Ok,Let's see: I was reading an article that When...
6
by: David Young | last post by:
I'm using hashtables to transport data between layers in my app, but in so doing, it seems almost impossible to avoid unboxing. private static BusinessObject PackageBusinessObject(Hashtable...
8
by: Peter Olcott | last post by:
Exactly why does C# and .NET require all the extra copying of data?
5
by: chenedor | last post by:
Hi all I am a bit confuse about unboxing... what is the difference and what is best practice ? object o = __box(1234); int n = *dynamic_cast<__box int*>(o); of
9
by: | last post by:
Execuse me. I do boxing, for example Employee class, as object: public object ManipulateObject(object objInput) { ... Object obj; obj = objInput; ...
94
by: Peter Olcott | last post by:
How can I create an ArrayList in the older version of .NET that does not require the expensive Boxing and UnBoxing operations? In my case it will be an ArrayList of structures of ordinal types. ...
19
by: ahjiang | last post by:
hi there,, what is the real advantage of boxing and unboxing operations in csharp? tried looking ard the internet but couldnt find any articles on it. appreciate any help
161
by: Peter Olcott | last post by:
According to Troelsen in "C# and the .NET Platform" "Boxing can be formally defined as the process of explicitly converting a value type into a corresponding reference type." I think that my...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.