Debugging with pointers in C #

When I programmed in C / C ++, I often include pointer values ​​in the trace. This helped match objects between trace messages. In C #, I don't have a pointer value that I can track. Are there any good alternatives for identifiers of traced objects?

I don’t know how to get the main memory address, but even if I did, this would not help, since the GC could move objects around.

+3
source share
3 answers

Add a class variable that increases with every constructor call. Assign this class variable as your own index for the class.

class foo
{
  private static int counter;
  private int instance_counter;

  public Foo()
  {
    instance_counter = counter;
    counter++;
  }

  public string toString()
  {
    return "Foo "+instance_counter.toString();
  }
}

can't compile out of the box, I don't have a compiler here.

+2

Object.GetHashCode(). GetHashCode() , # Object. Object.GetHashCode() - - . , , Hashcode .

+2

Source: https://habr.com/ru/post/1704478/


All Articles