When debugging your application, you may need to keep track of some out-of-scope data. Suppose you call MethodB() from MethodA() and perform an operation inside MethodB(), now when you return to MethodA(), all the local variables inside MethodB() are out of scope. But you want to use them in MethodA(). Now here’s the trick: you can create an ObjectID for a member variable or object and also track it when it goes out of scope, but until Garbage collector doesn’t collect it. In this article, I’ll explore how we can use out-of-range object values ​​to handle conditional breakpoints. To get started, you first need to understand how to use object IDs to track and scope an object. Here is my full article How to track an out-of-scope object when debugging?
Consider we have the code block below

class Program
{
static void Main(string[] args)
{
 //Call Method A
  MethodA();
}
///
/// Methods A.
///
private static void MethodA()
{
//Do Some Operation
//Call Method B
  MethodB();
// Do Some Operation
// Create a local List object of Students
List studs = new List{
new Student{Roll=1,Name="Abhik"},
new Student{Roll=2, Name="Abhijit" },
new Student { Roll = 3, Name = "Abhishek" },
new Student { Roll = 4, Name = "Rahul" },
new Student { Roll = 5, Name = "Ritaban" },
};
// Print them in Console
foreach (Student stud in studs)
{
   Console.Write(stud.ToString());
}
}
///
/// Methods the B.
///
private static void MethodB()
{
// Create a local Object of Student
Student stud = new Student { Roll = 1, Name = "Abhijit" };
}
}

///
/// Student Class
///
class Student
{
public int Roll { get; set; }
public string Name { get; set; }
public override string ToString()
{
return "Roll : " + this.Roll.ToString() + " Name : " + this.Name + "\n";
}
}

Well, that’s pretty simple code. The goal is to see how things work with an out-of-reach object. Now coming to the point of precision, you want to check the data value before printing the data to the console in MethodA() and you want to make sure that your breakpoint will only stop if the Student’s name is the same as the object created in MethodB ( ). So you need to set a conditional stop here. But is a conditional stop enough to stop your stop? yes, but you need to handle an object that is not in the current scope.

So first of all, you need to set a breakpoint on MethodB() where you create the Student object. and you need to create an object ID for the same. (From the Watch window > right click on object > Create Object ID)

How to use out of scope object within Conditional Breakpoint?

From the image above, you can see how we created the object ID (1#) from the “stud” object in MethodB().

Now, the next step is, using the Object ID in Conditional Breakpoint at MethodA().

How to use out of scope object within Conditional Breakpoint?

That’s it, now your breakpoint at “Console.WriteLine” will appear only when the name of the local student object is the same as the stud object in the name of MethodB() (1#).

How to use out of scope object within Conditional Breakpoint?

In the image above, you can see the breakpoint stops when the object ID name (1#) is the same as the current object name value. If you have multiple names matching the object ID value, your breakpoint will break multiple times.

Important Note:

After creating the object ID, if you stop and start debugging, you need to regenerate the object ID. otherwise you will get the error “Object ID not found” and also at the time when the garbage collector collects the object.

How to use out of scope object within Conditional Breakpoint?

I hope above will help. You can check more articles on our website under C# section HERE or you can also check same on Microsoft Docs too.

Explore More

Working With File Class In C#

In C#, ‘File’ class provides many static methods for moving, copying, and deleting files. These Static methods involve moving a file and copying and deleting a file also. Let’s check

How to Read a Text File in C#

In C# ‘File’ class provides static methods to read given text file data. The File.ReadAllText() method opens a text file, reads all the text present in the file into a

C# | How to use Interface references

In C#, you’re permitted to make a reference variable of an interface type or in other words, you’re permitted to form an interface reference variable. Such kind of variable can