Results 1 to 5 of 5

Thread: Something I didn't understand

  1. #1

    Thread Starter
    Hyperactive Member r0k3t's Avatar
    Join Date
    Dec 2005
    Location
    Cleveland
    Posts
    361

    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
    Anti DUPLO machine!!!

  2. #2
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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:
    csharp Code:
    1. int number = 100;
    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:
    1. 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:
    vb.net Code:
    1. object number = 100;
    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    Hyperactive Member r0k3t's Avatar
    Join Date
    Dec 2005
    Location
    Cleveland
    Posts
    361

    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!
    Anti DUPLO machine!!!

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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:
    1. int num1 = 100; // num1 is type int.
    2. var num2 = 101; // num2 is type int, inferred from the value used to initialise it.
    3. object num3 = 102; // num3 is type object.  It's a reference that refers to a boxed int value.
    4. int num4; // num4 is type int with no initial value.
    5. var num5; // COMPILATION ERROR!! A type cannot be inferred so the var keyword is not allowed.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width