Print the value of a object's memory address?

Does anyone know the C# equivalent of...

cout << &MyClass;

?

I need to print the memory address of an object in C#.  I've tried using unsafe code and the fixed statement which will let me set a primitive or unmanaged object to a pointer, but I still can't get the value of the memory address.  

Please help.  

ChrisClementAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

devsolnsCommented:
Not possible to get the memory address of an object in c#.  If even you did it wouldnt do you any good because the object can be moved around in memory by the garbage collector to keep the heap from fragmenting.

-gp
0
Carl TawnSystems and Integration DeveloperCommented:
How about:

        static void Main(string[] args)
        {
            int x = 20;
            int y = 30;

            unsafe
            {
                Console.WriteLine((int)&x);
                Console.WriteLine((int)&y);
            }

            Console.ReadLine();
        }
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
devsolnsCommented:
ok but keep in mind that is not code running within the CLR.  it runs as native code so keep that in mind.
0
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

Carl TawnSystems and Integration DeveloperCommented:
And if you are using pointers the you will need to use the "fixed" keyword in order to prevent the GC from invalidating your pointer.
0
ChrisClementAuthor Commented:
But how would you do this with objects rather than primitives?
0
ChrisClementAuthor Commented:
Nevermind.  I figured it out.  I kept getting an error when trying to get the address of a class.   The error was "cannot take the address or size of a variable of a managed type".  Did some checking and found that a struct would work just as well and it can be used in this way.

For example...

      class Class1
      {

            [STAThread]
            static void Main(string[] args)
            {
                  

                  unsafe
                  {
                        Class2 c = new Class2();
                        Class2 d = new Class2();
                        Console.WriteLine((int)&c);
                        Console.WriteLine((int)&d);
                  }

                  Console.ReadLine();
            }

      
            public struct Class2
            {
                  //example struct
            }
      }

Thanks for the help
0
devsolnsCommented:
fyi, Structs are on the stack and not the heap.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.