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 3998708
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T07:33:38+00:00 2026-05-20T07:33:38+00:00

Hy! Iam learning refference types now, and I dont get it why does x

  • 0

Hy!
Iam learning refference types now, and I dont get it why does x has the same memory address as y? Shouldn’t they have different addresses?

class Program
{
    static void Main(string[] args)
    {
        int x = 10; // int -> stack
        int y = x; // assigning the value of num to mun. 
        DisplayMemAddress(x);
        DisplayMemAddress(y);
        Console.ReadLine();
    }
    static unsafe void DisplayMemAddress(int x)
    {
        int* ptr = &x;
        Console.WriteLine("0x" + new IntPtr(ptr).ToString("x"));
    }
}
  • 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-20T07:33:38+00:00Added an answer on May 20, 2026 at 7:33 am

    x and y in Main are independent variables. They can store different values. They can’t be at the same address. Note that they’re value type variables though – you’re not actually learning about reference types at all in this code, as it doesn’t use any reference types (except string, Program and Console).

    However, your code doesn’t show that – it’s showing the address of the parameter in DisplayMemAddress, which is entirely different. The values of x and y are passed by value into the method. It would be helpful if you’d rename the parameter in your DisplayMemAddress method to z:

    static unsafe void DisplayMemAddress(int z)
    {
        int* ptr = &z;
        Console.WriteLine("0x" + new IntPtr(ptr).ToString("x"));
    }
    

    Now it’s easier to talk about. You’re displaying the address of z, not x or y. That address will be on the stack (as an implementation detail) and as the stack is the same height in both calls, it’ll show the same value. Now if you change the method to use pass-by-reference, you’ll actually see the address of x and y:

    class Program
    {
        static void Main(string[] args)
        {
            int x = 10; // int -> stack
            int y = x; // assigning the value of num to mun. 
            DisplayMemAddress(ref x);
            DisplayMemAddress(ref y);
            Console.ReadLine();
        }
    
        static unsafe void DisplayMemAddress(ref int z)
        {
            fixed (int* ptr = &z)
            {
                Console.WriteLine("0x" + new IntPtr(ptr).ToString("x"));
            }
        }
    }
    

    To be honest, showing addresses isn’t the best way of learning about reference types, value types and parameter passing IMO.

    I have a couple of articles you might find useful though:

    • Parameter passing
    • Value types and reference types
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am learning Objective-C right now, however, there is memory management puzzle here make
I am learning a lot about memory management but this one problem has me
I am learning Python for a class now, and we just covered tuples as
I am learning LINQ and have a very simple question that I think will
I am learning about SOAP implementation and have become somewhat confused regarding the appropriate
I am learning how to use NUnit. I have my main project in it's
I am learning LINQ and this seems like a fairly simple problem. I have
Good evening everyone! I am working on learning some java and I have made
recently I am learning C++ and have some doubt on the following case. void
I am learning WCF and right now I am getting one exception while running

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.