|
-
Jun 24th, 2009, 01:46 PM
#1
Thread Starter
Hyperactive Member
Something I didn't understand
I am not sure that I understand this and my attempts to get more information on it didn't help...
This is from Scott Gu's blog
Code:
Because the CLR supports automatic boxing/unboxing of value-classes
what is boxing/unboxing? and what is a value-class? or does he mean, values and classes?
Thanks
-
Jun 24th, 2009, 02:45 PM
#2
Re: Something I didn't understand
there are two types of data containers: Value and Object ... value types tend to be your basic types... integer, decimal, booleans, etc. Object types are considered complex and usually need to be created, SQLCommand, OLDEBConnection, etc. Because everything in .NET is an "object" even value items are implemented as classes. This is what lets you do things like mysomeIntNumber.ToString() and get a result. There's an Integer value-class the defines how it behaves. So, what you end up with are Object-Classes and Value-Classes. Clear? I hope. Then there's strings.... but that will only confuse things, so I'll skip it for now.
Boxing & unboxing.
This has to do with what's stored on the heap vs the stack and how the data is moved back and forth. I hope I have this right... some one save me if I don't get it right.
Value-based items are stored on the stack directly, so when you need a value, it goes to the stack, grabs it, and pulls it back. Objects on the other hand can't exist on the stack - they reside on the heap instead. But you don't just go out to the heap and pull back the data. So, the data for the object is pushed onto the heap, and a pointer to the data is returned, and THAT is pushed onto the stack. So when you need something in an object .NET will go to the stack, get the address, go to that location in the heap, box the data, bring it back to the stack, where it then unboxes what you needed and returns the value.
does that help any? And I hope I got it right.
-tg
-
Jun 24th, 2009, 08:04 PM
#3
Re: Something I didn't understand
Not that tg is wrong but here's my explanation. All variables are stored on the stack. For value type variables, i.e. all structures and enumerations, the variable contains the object itself. For instance, if you do this:the 'number' variable is created on the stack and the value 100 is stored in that variable. For reference type variables, i.e. all classes and delegates, the variable contains a reference to, i.e. the memory address of, an object on the heap. For instance, if you do this:
csharp Code:
DataTable table = new DataTable();
the 'table' variable is created on the stack, a DataTable object is created on the heap and then the heap address is stored in the stack variable.
Now, this is an example of boxing:Object is a class so that is creating a reference type variable, but it's assigning a value type object to it. Boxing is basically wrapping a value type object in a reference. It's inefficient so is to be avoided in cases where it's not required.
-
Jun 25th, 2009, 09:34 AM
#4
Thread Starter
Hyperactive Member
Re: Something I didn't understand
Sweet -
Those explanations really where both very good. What I take from that is: use value types (what Scott Guthrie referred to as value-classes) whenever possible.
The only other thing this made me wonder about is the new var keyword (I believe keyword is the most correct way to address it). Look at the following code
Code:
int number = 100; // value-class created and placed on the stack
Person theDude = new Person(); // Object - created and placed on the heap (needs boxing/unboxing)
////////////////
var otherNum = 101; // ??? value-class created and placed on the stack
var someOtherDude = new Person(); // Object - created and placed on the heap (needs boxing/unboxing)
The question is this are those two really the same? Does the compiler know that otherNum is still an int and go ahead and create it as a value based class? or have I now created an integer that has to be boxed and unboxed by the CLR?
OK - Thanks!
-
Jun 25th, 2009, 09:47 AM
#5
Re: Something I didn't understand
It is certainly not the case that you should use value types wherever possible. The .NET Framework contains far more classes than structures. It is often a good idea to declare small, simple types as structures because they are quick and easy to manipulate but, whenever you assign a value from one variable to another, you are copying the entire object so they are not good for larger objects. The rule of thumb is that any object that is larger than 16 bytes in size should be a reference type.
Your code is not correct, or at least the comments aren't. Boxing ONLY occurs when you assign a value type object to an Object variable. There's no such thing as boxing of reference types so creating a Person object cannot involve boxing in any way, shape or form.
The 'var' key word invokes type inference. Any variable declared with the 'var' keyword is still strongly-typed. It's just that the type is inferred by the compiler from the expression used to initialise the variable. If a type, other than Object, cannot be inferred from an initialising expression then the 'var' key cannot be used.
csharp Code:
int num1 = 100; // num1 is type int. var num2 = 101; // num2 is type int, inferred from the value used to initialise it. object num3 = 102; // num3 is type object. It's a reference that refers to a boxed int value. int num4; // num4 is type int with no initial value. var num5; // COMPILATION ERROR!! A type cannot be inferred so the var keyword is not allowed.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|