Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using LINQ expressions in Visual Studio's Watch window

I have a byte[] variable in program, e.g.:

byte[] myByteArray = new byte[] { 0xF0, 0x0F };

When debugging this program, I wanted to display the byte array content as individual hexadecimal values inside Visual Studio's Watch window.

So I tried to use the following LINQ expression in the Watch Window, without success:

myByteArray.Select(value => value.ToString("X2")).ToArray()

Watch window's error message:

error CS1061: 'byte[]' does not contain a definition for 'Select' and no extension method 'Select' accepting a first argument of type 'byte[]' could be found (are you missing a using directive or an assembly reference?)

Does anyone know if there is a way to use LINQ expressions in Visual Studio's Watch window without installing third-party extensions?

I'm using VS2017 15.6.6 at this moment.

Edit: A screenshot of this issue...

enter image description here

like image 804
sɐunıɔןɐqɐp Avatar asked Apr 24 '18 08:04

sɐunıɔןɐqɐp


People also ask

How do I display a watch window in Visual Studio?

Select the variable a in the code. Select Debug > QuickWatch, press Shift+F9, or right-click and select QuickWatch.

How do I quick watch in Visual Studio?

I search on Google about "Visual Studio 2019 Watch Window". It tells me that open the Watch Window by the menu sequence Debug > Windows > Watch.


2 Answers

I tried to reproduce your problem and found the following:

It seems the watch window uses the namespaces you referenced (via using) in your code.

If you don't use linq (and System.Linq namespace) in the code file, the watch window cannot find the extensions.

If you have a using System.Linq; and use something from that namespace in your code, the watch window will find and execute the linq extensions. (If you don't use anything from System.Linq the reference is optimized away, so this assembly is not loaded at runtime and the debugger can't use it).

like image 114
René Vogt Avatar answered Oct 12 '22 00:10

René Vogt


If you don't have a 'using System.Linq' statement in the code, you can still use Linq queries by calling the extension methods manually:

System.Linq.Enumerable.Select(collection, x=>x.Name)
like image 29
baddie Avatar answered Oct 12 '22 01:10

baddie