0

Since I started to be a programmer, I always worked with "High level" languages with no memory interruption by me. currently I'm C# Developer and I have been wondering if there is a way to see "the inside" of my code, I mean to see the addresses of my variables etc.

I just want to improve my understanding as programmer.

Óscar Andreu
  • 1,630
  • 13
  • 32
CPU
  • 91
  • 1
  • 7
  • 2
    IL code, it's not a game! – Drag and Drop Oct 31 '18 at 08:34
  • You could start by [viewing your program's IL](https://stackoverflow.com/questions/9204/il-level-code-debugger) – John Wu Oct 31 '18 at 08:34
  • 1
    Possible duplicate of [How can I view MSIL / CIL generated by C# compiler? Why is it called assembly?](https://stackoverflow.com/questions/3326571/how-can-i-view-msil-cil-generated-by-c-sharp-compiler-why-is-it-called-assemb) – Drag and Drop Oct 31 '18 at 08:35
  • 1
    Bear in mind that a lot of your variables (assuming normal usage of C#) won't have *an* address. They'll be member fields of a class, or local variables of a method, and as such, at any one time, there may be *multiple* instances of these variables. (Or none, if nothing is running the method/no instances of the class exist) – Damien_The_Unbeliever Oct 31 '18 at 08:46

1 Answers1

4

You just need to use the unsafe keyword

Also if you use resharper you can easily view the IL

Resharper -> windows -> IL Viewer

int number = 234;

unsafe 
{
    // Assign the address of number to a pointer:
    int* p = &number;

    // Print the value of *p:
    System.Console.WriteLine("Value at the location pointed to by p: {0:X}", *p);

    // Print the address stored in p:
    System.Console.WriteLine("The address stored in p: {0}", (int)p);
}

Additional Resources

How to: obtain the address of a variable (C# Programming Guide)

To use unsafe, you need to explicitly set the build setting

-unsafe (C# Compiler Options)

To set this compiler option in the Visual Studio development environment

  • Open the project's Properties page.

  • Click the Build property page.

  • Select the Allow Unsafe Code check box.

TheGeneral
  • 79,002
  • 9
  • 103
  • 141
  • Are adresses in IL code really physical adresses or a runtime abstraction? Haven't cared for this so long, that I am not sure any more. I figure you would know from the top of your head ... – Fildor Oct 31 '18 at 08:45
  • No IL doesn't show the actual physical memory addresses, It is the Intermediate Language .Net uses to run your application, from here it gets converted to PE code which will have the actual addresses ect – TheGeneral Oct 31 '18 at 08:48
  • That's what I thought. I was just asking because OP may have the impression he'd be able to see actual physical adresses in IL code. May be worth a clarification? – Fildor Oct 31 '18 at 08:50
  • @Fildor, IL is not the physical, I read the question as whats behind the scene and IL is a great tool for that. – Drag and Drop Oct 31 '18 at 08:58
  • @DragandDrop _"I mean to see the addresses of my variables etc"_ that's what I was referring to. Outside that I agree. Not sure what OP is expecting to see, exactly. That's why I thought it would be worth it to include what he can _and cannot_ expect when looking into IL code. The title of the question is also a hint ... – Fildor Oct 31 '18 at 09:05