473,320 Members | 1,814 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,320 software developers and data experts.

Implicit UnBoxing

I am a begineer in C#. I wanted to know why implicit unboxing is not allowed?
Jan 8 '09 #1
5 2492
r035198x
13,262 8TB
Give an example of what you mean.
Note that in C# even primitive types extend from the Object class so you can call all the methods in the object class without needing to type cast the primitives to object class type.
Jan 8 '09 #2
For Example:
Expand|Select|Wrap|Line Numbers
  1. int i= 3;
  2. object o = i;//Implicit boxing
  3. int j = o;//Implicit unboxing,does not compile.
if object o stores the type (int), why cant we perform implicit unboxing? here?
Jan 8 '09 #3
r035198x
13,262 8TB
Because type int is a more specific type of type object. In general you can never implicitly cast from a general type to a more specific type in strongly typed languages. Try it with any inheritance hierarchy. It has nothing to do with autoboxing.
Jan 8 '09 #4
Curtis Rutland
3,256 Expert 2GB
Explicit casting is just part of the language. It's safe, because it guarantees that you will only be putting something that fits in the box in it.

Effectively what this means is that with C#, you have to tell it exactly what you want it to do. It won't try to automatically cast your objects for you. It means you have greater control over your programming, but you also have to remember to manually do a few things.

Explicit casting also builds good habits, and makes for much more readable code.
Jan 8 '09 #5
mldisibio
190 Expert 100+
The compiler intentionally insures that the object's type can be guaranteed, and if not, then your are responsible for the application blowing-up, not the compiler.

It is trying to protect against the following:

Expand|Select|Wrap|Line Numbers
  1. int i= 3;
  2. object o = i;//Implicit boxing
  3.  
  4. // somewhere else in your code, 
  5. // the address of object "o" is now assigned to a new string
  6. o = "oops";
  7.  
  8. // meanwhile back at the ranch...
  9. // Compiler knows o could be anything. Says, "I'm not your babysitter."
  10. int j = o; //Implicit unboxing,does not compile.
  11. int j = (int)o; // Compiles, and will blow. Your fault!
  12. string s = (string)o; // Compiles. Compiler happy. Customer happy.
  13.  
Line 11 will throw a System.InvalidCastException at runtime.
Jan 8 '09 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: cody | last post by:
hi! i always hear about the dangers (performance loss) that comes with implicit autoboxing. when are situations where this happens? i only know this happens when i assign a value type to a...
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...
3
by: Steve | last post by:
Hi, I have a class like: public ClassA { int vals1; int vals2; }
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...
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
13
by: gökhan | last post by:
Hi! I fear this question might be too basic, however being a c++ veteran I have trouble to get a good desing in c# running. // a base class class Vector{}; // inherited class class...
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: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.