How does CLR differentiates between value and reference types
-
Hi, Basic question to ask. we know everything in .Net is derived from System.Object, even value and reference types. So, how does CLR differentiate between the two and stores the value type on Stack and reference types on Heap? In continuation to above, I have another query we have a value type as
int age = 35;
and we have a class which contains both the value types and reference types as shown below
Class Company
{
int employeeCount = 3000;
Employee emp;Public Company(Employee emp) { this.emp = emp; }
}
and we created the object of Company class as shown below
Company cmpny = new Company(emp);
Question is where does the employeeCount will get stored(Stack or Heap) and Where does the cmpny gets stored? Thanks in advance,
Praveen Raghuvanshi Software Engineer, India.
-
Hi, Basic question to ask. we know everything in .Net is derived from System.Object, even value and reference types. So, how does CLR differentiate between the two and stores the value type on Stack and reference types on Heap? In continuation to above, I have another query we have a value type as
int age = 35;
and we have a class which contains both the value types and reference types as shown below
Class Company
{
int employeeCount = 3000;
Employee emp;Public Company(Employee emp) { this.emp = emp; }
}
and we created the object of Company class as shown below
Company cmpny = new Company(emp);
Question is where does the employeeCount will get stored(Stack or Heap) and Where does the cmpny gets stored? Thanks in advance,
Praveen Raghuvanshi Software Engineer, India.
1- All the value types are derived from System.Valuetype and are stored in the stack. For other type of object needs to be initialized and are stored in the managed heap with all of its data members. 2- All the value type present in an object resides in managed heap together. They are not stored separately in the stack.
WWW, WCF, WWF, WPF, WFC .... WTF
-
Hi, Basic question to ask. we know everything in .Net is derived from System.Object, even value and reference types. So, how does CLR differentiate between the two and stores the value type on Stack and reference types on Heap? In continuation to above, I have another query we have a value type as
int age = 35;
and we have a class which contains both the value types and reference types as shown below
Class Company
{
int employeeCount = 3000;
Employee emp;Public Company(Employee emp) { this.emp = emp; }
}
and we created the object of Company class as shown below
Company cmpny = new Company(emp);
Question is where does the employeeCount will get stored(Stack or Heap) and Where does the cmpny gets stored? Thanks in advance,
Praveen Raghuvanshi Software Engineer, India.
Rags1512 wrote:
we know everything in .Net is derived from System.Object, even value and reference types.
All value types are derived implicitly from the System.ValueType, which is derived from object.
Rags1512 wrote:
Question is where does the employeeCount will get stored(Stack or Heap) and Where does the cmpny gets stored?
The answer is, where a value type is declares determines where it ends up. The
cmpy
object is stored in the heap, but with a pointer from the stack.employeeCount
is stored in the heap memory, but is accessed [transparently] through a pointer on the stack. Here is a good article on heap/stack allocation http://www.c-sharpcorner.com/UploadFile/rmcochran/csharp_memory01122006130034PM/csharp_memory.aspx[^] Though the author fudges the point about thecmpy
pointer a little.Dalek Dave: There are many words that some find offensive, Homosexuality, Alcoholism, Religion, Visual Basic, Manchester United, Butter. Pete o'Hanlon: If it wasn't insulting tools, I'd say you were dumber than a bag of spanners.
-
Rags1512 wrote:
we know everything in .Net is derived from System.Object, even value and reference types.
All value types are derived implicitly from the System.ValueType, which is derived from object.
Rags1512 wrote:
Question is where does the employeeCount will get stored(Stack or Heap) and Where does the cmpny gets stored?
The answer is, where a value type is declares determines where it ends up. The
cmpy
object is stored in the heap, but with a pointer from the stack.employeeCount
is stored in the heap memory, but is accessed [transparently] through a pointer on the stack. Here is a good article on heap/stack allocation http://www.c-sharpcorner.com/UploadFile/rmcochran/csharp_memory01122006130034PM/csharp_memory.aspx[^] Though the author fudges the point about thecmpy
pointer a little.Dalek Dave: There are many words that some find offensive, Homosexuality, Alcoholism, Religion, Visual Basic, Manchester United, Butter. Pete o'Hanlon: If it wasn't insulting tools, I'd say you were dumber than a bag of spanners.
Appreciate the explanation provided,
Praveen Raghuvanshi Software Engineer, India.
-
Hi, Basic question to ask. we know everything in .Net is derived from System.Object, even value and reference types. So, how does CLR differentiate between the two and stores the value type on Stack and reference types on Heap? In continuation to above, I have another query we have a value type as
int age = 35;
and we have a class which contains both the value types and reference types as shown below
Class Company
{
int employeeCount = 3000;
Employee emp;Public Company(Employee emp) { this.emp = emp; }
}
and we created the object of Company class as shown below
Company cmpny = new Company(emp);
Question is where does the employeeCount will get stored(Stack or Heap) and Where does the cmpny gets stored? Thanks in advance,
Praveen Raghuvanshi Software Engineer, India.
Rags1512 wrote:
So, how does CLR differentiate between the two and stores the value type on Stack and reference types on Heap?
That value types are always on the stack and reference types are always on the heap is a common misconception. The distinction between value types and reference types is not where they are allocated (stack vs. heap), the difference is how they are passed to functions and assignment. Value types are passed by value. Reference types are passed by reference. An article discussing this can be found here
Rags1512 wrote:
we know everything in .Net is derived from System.Object, even value and reference types.
Also, not quite true. See this article for a good explanation.
-
Hi, Basic question to ask. we know everything in .Net is derived from System.Object, even value and reference types. So, how does CLR differentiate between the two and stores the value type on Stack and reference types on Heap? In continuation to above, I have another query we have a value type as
int age = 35;
and we have a class which contains both the value types and reference types as shown below
Class Company
{
int employeeCount = 3000;
Employee emp;Public Company(Employee emp) { this.emp = emp; }
}
and we created the object of Company class as shown below
Company cmpny = new Company(emp);
Question is where does the employeeCount will get stored(Stack or Heap) and Where does the cmpny gets stored? Thanks in advance,
Praveen Raghuvanshi Software Engineer, India.
Rags1512 wrote:
will get stored(Stack or Heap)
We really don't care. If you're interested in performance, don't use a managed system at all.
-
Hi, Basic question to ask. we know everything in .Net is derived from System.Object, even value and reference types. So, how does CLR differentiate between the two and stores the value type on Stack and reference types on Heap? In continuation to above, I have another query we have a value type as
int age = 35;
and we have a class which contains both the value types and reference types as shown below
Class Company
{
int employeeCount = 3000;
Employee emp;Public Company(Employee emp) { this.emp = emp; }
}
and we created the object of Company class as shown below
Company cmpny = new Company(emp);
Question is where does the employeeCount will get stored(Stack or Heap) and Where does the cmpny gets stored? Thanks in advance,
Praveen Raghuvanshi Software Engineer, India.
Do you want a simple answer or all the gory details[^]? Simple:
Rags1512 wrote:
how does CLR differentiate between the two
It's in the metadata, see .NET file format - Signatures under the hood, Part 1 of 2[^]
Rags1512 wrote:
stores the value type on Stack and reference types on Heap
That's very misleading, reference types end up putting something in the heap as well as on the stack and value types may well be in the heap (as a field of a reference type, for example). The (well "a") difference is that an instance of a value type can be on the stack, whereas an instance of a reference type is always in the heap (but it may have a reference to it on the stack)
Rags1512 wrote:
Question is where does the employeeCount will get stored(Stack or Heap) and Where does the cmpny gets stored?
employeeCount
will be in the heap, inside the instance of theCompany
it is part of. Wherecmpny
is stored is impossible to tell without more context. If that line is part of the declaration of a reference type (field with initializer) it will be in the heap as part of it's parent object, if that like is part of the declaration of a value type it may end up on the stack (or the instance of the value type it is in could be a field of a reference type), if that line is inside a method (local variable with initializer) it will definitely be on the stack. But the instance of theCompany
thatcmpny
refers to will always be in the heap. I think. -
Hi, Basic question to ask. we know everything in .Net is derived from System.Object, even value and reference types. So, how does CLR differentiate between the two and stores the value type on Stack and reference types on Heap? In continuation to above, I have another query we have a value type as
int age = 35;
and we have a class which contains both the value types and reference types as shown below
Class Company
{
int employeeCount = 3000;
Employee emp;Public Company(Employee emp) { this.emp = emp; }
}
and we created the object of Company class as shown below
Company cmpny = new Company(emp);
Question is where does the employeeCount will get stored(Stack or Heap) and Where does the cmpny gets stored? Thanks in advance,
Praveen Raghuvanshi Software Engineer, India.
-
Check this link: Difference between Value Type and Reference Type?[^] Reply me about how is it? Helpful or not
It was helpful. Thanks!
Praveen Raghuvanshi Software Engineer, India.
-
It was helpful. Thanks!
Praveen Raghuvanshi Software Engineer, India.
check this link again http://opexsolution.com/forum/viewtopic.php?f=18&t=8 you will get more clear idea about Reference and Value types