简体   繁体   中英

Using LINQ expressions in Visual Studio's Watch window

I have a byte[] variable in program, eg:

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...

在此处输入图片说明

如果代码中没有 'using System.Linq' 语句,您仍然可以通过手动调用扩展方法来使用 Linq 查询:

System.Linq.Enumerable.Select(collection, x=>x.Name)

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).

在此处输入图片说明

Try to add the following: (Its working on my test)

> using System.Collections.Generic;
 using System.Linq;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM