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...
Select the variable a in the code. Select Debug > QuickWatch, press Shift+F9, or right-click and select QuickWatch.
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.
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).
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With