Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 471085
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T23:57:01+00:00 2026-05-12T23:57:01+00:00

Hi I have some doubts in the memory allocation of the reference types.Please clarify

  • 0

Hi I have some doubts in the memory allocation of the reference types.Please clarify my questions that are commented in between the code below.

 class Program
    {
        static void Main(string[] args)
        {
            testclass objtestclass1 = new testclass();
            testclass objtestclass2 = new testclass();
            testclass objtestclass3 = new testclass();
            // Is seperate memory created for all the three objects that are created above ? 
            objtestclass1.setnumber(1);
            objtestclass2.setnumber(2);
            Console.Write(objtestclass1.number);
            Console.Write(objtestclass2.number);
            objtestclass3 = objtestclass1;
            //When we assign one object to another object is the existing memory of       the         objtestclass3 be cleared by GC
            Console.Write(objtestclass3.number);
            objtestclass3.setnumber(3);
            Console.Write(objtestclass3.number);
            Console.Write(objtestclass1.number);
            Console.Read();   
            }

            public class testclass
            {
                public int number = 0;
                public void setnumber(int a)
                {
                    number = a;
                }

            }

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T23:57:01+00:00Added an answer on May 12, 2026 at 11:57 pm

    The instance of testclass is on the heap. Each instance will consist of:

    • A sync block
    • A type reference
    • The field number

    On a 32-bit Windows .NET, this will take 12 bytes.

    The local variables within the Main method (objtestclass1 etc) will be on the stack – but they’re references, not objects. Each reference will be 4 bytes (again on a 32-bit CLR).

    The difference between references and objects is important. For example, after this line:

    objtestclass3 = objtestclass1;
    

    You’re making the values of the two variables the same – but those values are both references. In other words, both variables refer to the same object, so if you make a change via one variable you’ll be able to see it via the other variable. You can think of the reference as a bit like a URL – if we both have the same URL, and one of us edits the page it refers to, we’ll both see that edit.

    For more information on this, see my article on reference types and another one on memory.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some simple doubts about NSOperation and GCD that I have not found
Now that I've read all the DQL docs I still have some doubts, I'm
I have some doubts. What is querystrng? how we can use it? what are
I have some doubts about construction and initialization order guarantees in C++. For instance,
I have programmed an emulator, but I have some doubts about how to organizate
im currently learning python (in the very begining), so I still have some doubts
I have two doubts: 1. I would like to add some client side support
I have some UI in VB 2005 that looks great in XP Style, but
I have some code for starting a thread on the .NET CF 2.0: ThreadStart
We have some input data that sometimes appears with &nbsp characters on the end.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.