Almost all software developers using Visual Studio are well aware of the most common debugging functions provided in Visual Studio. For example, functionality like “Step Into”, “Step Over” and “Step Out” as shown in figure 1 are few tasks that developers perform quite frequently during their day to day job.
However, with the advent of use of Fluent APIs and method chaining techniques (heavily used in frameworks like Linq), some of these tasks can become quite repetitive and cumbersome. Let’s say, you have extended the FileInfo class using Extension methods as follows:
public static class FileInfoExtension
{
public static IEnumerable<FileInfo>
IsAcceptableFileType(this IEnumerable<FileInfo> files)
{
foreach (FileInfo file in files)
{
if (file.Extension == ".jpg" || file.Extension == ".png" )
yield return file;
}
}
public static IEnumerable<FileInfo>
IsAcceptableFileSize(this IEnumerable<FileInfo> files)
{
foreach (FileInfo file in files)
{
if ( file.Length < 1024*2024)
yield return file;
}
}
public static IEnumerable<FileInfo>
IsAcceptableFileName(this IEnumerable<FileInfo> files)
{
foreach (FileInfo file in files)
{
if (file.Name.StartsWith("Figure"))
yield return file;
}
}
}
Nothing as a rocket science in this code, just filtering a List of FileInfo
class instances based on file type, file size and file name. Here is an example of how these extension methods are used in the code.
class Program
{
static void Main(string[] args)
{
string path = @"C:\my\CallstackRelatedDemo";
DirectoryInfo di = new DirectoryInfo(path);
if (di.Exists)
{
var results = di.GetFiles()
.ToList()
.IsAcceptableFileType()
.IsAcceptableFileSize()
.IsAcceptableFileName()
.Select( x => new
{
FileName = x.Name,
Extension = x.Extension,
SizeInKB = x.Length/1024
})
.ToList();
}
}
}
Let’s say during the debugging session, developer steps into the IsAcceptableFileType
method. At this point, call-stack will look as follows:
If developer wants to continue troubleshooting in IsAcceptableFileName
method, she can certainly use “Step Into” and “Step Over” functions but this could be a longer route to get to that method. An easier way is to use “Run To Cursor” approach available within the call-stack window. Right clicking in the call-stack window on the IsAcceptableFileName
, a context menu will be displayed as shown in figure 3 below.
Choosing “Run To Cursor” item from this context menu will take the developer directly to IsAcceptableFileName
method as shown below:
This way, you don’t have to click F10/F11/Ctrl+F11 keys multiple times and with just one click, you can start troubleshooting in the method you desire.
Until next, happy debugging.