{"id":249489,"date":"2024-06-18T08:34:17","date_gmt":"2024-06-18T15:34:17","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/visualstudio\/?p=249489"},"modified":"2024-07-01T06:40:17","modified_gmt":"2024-07-01T13:40:17","slug":"easily-navigate-code-delegates-while-debugging","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/visualstudio\/easily-navigate-code-delegates-while-debugging\/","title":{"rendered":"Easily navigate code delegates while debugging"},"content":{"rendered":"<p>Delegates are everywhere in modern code; a delegate is a type that represents references to methods with a particular parameter list and return type. Developers use delegates to pass methods as arguments to other methods. One example you may be familiar with is with event handlers. Handlers are methods you can invoke through delegates. Delegates remind me of C++ function pointers, but of course delegates are fully object-oriented.<\/p>\n<p>There are a few ways to represent delegates, there is, for example, <a href=\"https:\/\/learn.microsoft.com\/dotnet\/api\/system.func-1?view=net-8.0\"><strong>Func delegate<\/strong><\/a>. This generic delegate represents a method that takes one or more parameters and returns a value of a specified type. Here is an example (with a lambda expression):<\/p>\n<pre>Func&lt;int, int&gt; Multiplier = n =&gt; n * 5;<br \/>int val = Multiplier(5);<br \/>Console.WriteLine(val);<\/pre>\n<p>The most recent variation of this concept is <a href=\"https:\/\/learn.microsoft.com\/dotnet\/api\/system.action-1?view=net-8.0\"><strong>Action<\/strong><\/a>, which provides a more convenient shorthand. When you use <strong>Action<\/strong> you do not have to explicitly define a delegate that encapsulates a method with a single parameter. Here is an example:<\/p>\n<pre>Action&lt;string&gt; outputFunc = GetOutputRoutine();<br \/>outputFunc(\"Hello, World!\");<br \/><br \/>static Action&lt;string&gt; GetOutputRoutine()<br \/>{<br \/>\u00a0 \u00a0return MyConsoleWriter;<br \/>}<br \/><br \/>static void MyConsoleWriter(string input)<br \/>{<br \/>\u00a0 \u00a0Console.WriteLine(\"Console: {0}\", input);<br \/>}<\/pre>\n<p>So, this is a nice lesson, but why am I mentioning all this? While I find passing methods around like parameters is convenient when writing code, I also wish it were easier to follow while debugging. You can of course easily step into these methods, but I often want to quickly navigate to the underlying code represented by the delegate before or after stepping, and with the latest updates to Visual Studio 17.10 it\u2019s incredibly easy.<\/p>\n<p>When you pause while debugging, you can hover over any delegate and get a convenient go to source link, here is an example with a Func delegate.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2024\/06\/vs-debugging-func-goto-source.png\">