Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. How does CLR differentiates between value and reference types

How does CLR differentiates between value and reference types

Scheduled Pinned Locked Moved C#
questioncsharpdatabasedotnetdata-structures
10 Posts 7 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    Praveen Raghuvanshi
    wrote on last edited by
    #1

    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.

    P K G P L 6 Replies Last reply
    0
    • P Praveen Raghuvanshi

      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.

      P Offline
      P Offline
      PSK_
      wrote on last edited by
      #2

      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

      1 Reply Last reply
      0
      • P Praveen Raghuvanshi

        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.

        K Offline
        K Offline
        Keith Barrow
        wrote on last edited by
        #3

        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 the cmpy 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.

        P 1 Reply Last reply
        0
        • K Keith Barrow

          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 the cmpy 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.

          P Offline
          P Offline
          Praveen Raghuvanshi
          wrote on last edited by
          #4

          Appreciate the explanation provided,

          Praveen Raghuvanshi Software Engineer, India.

          1 Reply Last reply
          0
          • P Praveen Raghuvanshi

            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.

            G Offline
            G Offline
            Gideon Engelberth
            wrote on last edited by
            #5

            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.

            1 Reply Last reply
            0
            • P Praveen Raghuvanshi

              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.

              P Offline
              P Offline
              PIEBALDconsult
              wrote on last edited by
              #6

              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.

              1 Reply Last reply
              0
              • P Praveen Raghuvanshi

                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.

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #7

                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 the Company it is part of. Where cmpny 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 the Company that cmpny refers to will always be in the heap. I think.

                1 Reply Last reply
                0
                • P Praveen Raghuvanshi

                  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.

                  T Offline
                  T Offline
                  Tej Aj
                  wrote on last edited by
                  #8

                  Check this link: Difference between Value Type and Reference Type?[^] Reply me about how is it? Helpful or not

                  P 1 Reply Last reply
                  0
                  • T Tej Aj

                    Check this link: Difference between Value Type and Reference Type?[^] Reply me about how is it? Helpful or not

                    P Offline
                    P Offline
                    Praveen Raghuvanshi
                    wrote on last edited by
                    #9

                    It was helpful. Thanks!

                    Praveen Raghuvanshi Software Engineer, India.

                    T 1 Reply Last reply
                    0
                    • P Praveen Raghuvanshi

                      It was helpful. Thanks!

                      Praveen Raghuvanshi Software Engineer, India.

                      T Offline
                      T Offline
                      Tej Aj
                      wrote on last edited by
                      #10

                      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

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups