Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
This is a basic question which is confusing me..
Here is the sample code..

class Program
    {
        static void Main(string[] args)
        {
            c obj = new c();
            obj.a = 11;

        }
    }
    class c
    {
        public int a = 10;
    }


Now my question is we know integer is a value type and classes/objects are reference types..

Then obj.a now a is a value type of a reference type ?

since without boxing we can directly assign a value to obj.a = 11 which suggests that it is a value type.. but isn't "a" under the object obj which is reference type ?

How it is stored internally ?
Where is "a" stored ?
where is "obj" stored?

as far as I know a should be stored in a stack as it is value type and obj in heap as it is reference type.. but how they are associated ? can heap hold stack ? how does it work ?
Posted
Updated 3-Jan-11 0:04am
v2
Comments
Manfred Rudolf Bihy 4-Jan-11 7:51am    
Regarding you comment to my answer:
The site in your link says exactly what I had stated in my comment:
"... all local variables are values, but some contain a pointer into the heap and these variables with pointers stored in them are called references ..."
I really can't help it if you're unable to read and understand.

You've got lot more understanding on Object and reference type.

But I think this one statement will clear your confusion.

"value of a is referenced by each of object created from blue print(class) c"

For every object of c by assigning value of a will not directly contain value of a but rather it content reference address of a.

So that mean by changing a value of a will change value of a inside every object of class c.

I hope you may have now some idea around.
 
Share this answer
 
v3
Please read this on MSDN especially "ObjectInstance" and "Base Instance Size" that should clear you confusion: See How the CLR Creates Runtime Objects
[^]

If you still have some doubts, leave a comment.
But please read this article.

Regards,
Manfred
 
Share this answer
 
Comments
cooljeba1 3-Jan-11 7:14am    
Thanks for pointing to the article.. It pretty much explains a lot.. I did take some time to go through the article but to be frank I could understand most part except few.
One interesting point mentioned there was "An object can be referenced from stack-based local variables ".
So that's how it does ? The value types are stored in the stack and are then refrenced to the object ? So the object knows where to look for the value ?

So what happens when we use ref int c.. is int c still a value type ? or the ref keywords gets the address of the int c ?

THanks..
Manfred Rudolf Bihy 3-Jan-11 8:01am    
Not quite, that means that local variables are kept on the stack. When an object is stored into a local variable only a reference is stored. Look unto a reference as the same thing as pointers in C/C++. The object itself is stored in the heap and everything that is inside that object. When we pass objects into functions we're not passing the object itself but only a reference (pointer) of it. So one could say (simplified of course) that all local variables are values, but some contain a pointer into the heap and these variable with pointers stored in them are called references in the OO world.
For everything stored in one object storage must be allocated on the heap when an instance is created. That storage only is made up of the total size for all the value types plus the size of all references (pointers) to other objects and these other objects have their own memory block allocated for them.
I hope this cleared a little!
Don't hasten to ask if your unsure about what I said.
cooljeba1 3-Jan-11 8:40am    
Thanks for explaing that.. hmm this what I understand now (please correct me if I am wrong)
class c
{
public int a = 10;
}


c obj = new c();
when we create an instance the value (10) of "a" is stored in the stack

but when we update the value
obj.a = 11;

now obj.a contains the address of "a" in the stack or obj.a will directly hold the value 11 in heap ?
Manfred Rudolf Bihy 3-Jan-11 9:04am    
No! You'll have to reread my above comment. Everything that makes up the content of an object is stored on the heap. The stack only comes into play for local variables inside methods.
I can't express myself any clearer than in my comment above.
I'm sorry!
cooljeba1 4-Jan-11 1:52am    
Hi,

Thanks for your comments.. but yesterday I found another link please refer http://en.csharp-online.net/Value_vs_Reference
Here it shows that the address of the reference type is also stored in the stack.. only the reference type data is stored in heap ?
The field a is a value type that is a part of your class definition for c. obj.a is still a value type.

Exactly how it's stored internally is something we tend to disregard when we develop in C#. It can conceivably change between .Net versions. The “int a” is stored as a 32-bit integer though. The layout of Value types can be specified using attributes from the System.Runtime.InteropServices namespace.

Regards
Espen Harlinn
 
Share this answer
 
Comments
cooljeba1 3-Jan-11 7:31am    
Yes I tried that.. a.GetType().tostring() displays the type as Int32 which is a value type.. But my doubt was how are they tied together.. I mean the heap and the stack ?

Thanks

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900