C# 打印一个数组的内容(代码为一行,用于visual studio的立即窗口)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2030530/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 22:50:23  来源:igfitidea点击:

Print the contents of an array (code is one line, for use in Immediate window of visual studio)

c#.netvisual-studio

提问by MedicineMan

Can you write a convenient line of code that prints the contents of an array?

你能写一行方便的代码来打印数组的内容吗?

I will use this in the Immediate Window of Visual Studio 2008, so really it has to work in that window. I may have left out some requirements, but that's pretty much what I'm trying to do.

我将在 Visual Studio 2008 的即时窗口中使用它,因此它必须在该窗口中工作。我可能遗漏了一些要求,但这几乎就是我想要做的。

采纳答案by BFree

myArray.ToList().ForEach(Console.WriteLine);

Honestly though, I don't think that'll work in the immediate window. It is a nice trick to print it all in one line, but I think for the immediate window, all you need is this:

老实说,我认为这不会在直接窗口中起作用。将其全部打印在一行中是一个不错的技巧,但我认为对于直接窗口,您只需要这样:

? myArray

回答by mletterle

Might be easier to just use the watch tab. But simply typing the name of the array in the immediate tab should return the contents in a somewhat useful format.

仅使用监视选项卡可能更容易。但是,只需在立即选项卡中键入数组的名称,就会以某种有用的格式返回内容。

回答by Paul Creasey

where ais the array

a数组在哪里

?a

回答by Emmanuel

For both the Watch and Immediate windows in Visual Studio, the string returned by ToString() for an object will be used.

对于 Visual Studio 中的 Watch 和 Immediate 窗口,将使用 ToString() 为对象返回的字符串。

So you can override ToString() if you want and format the human-readable representation of any of your classes so that they display the information you need in the Watch or Immediate windows during debugging activities.

因此,您可以根据需要覆盖 ToString() 并格式化任何类的人类可读表示,以便它们在调试活动期间在 Watch 或 Immediate 窗口中显示您需要的信息。

For example,

例如,

public class Foo
{
   public String Bar { get; set; }
   private Int32 _intValue;
   public Int32 Value { get { return _intValue; } }
   override public ToString()
   {
      return "Bar: " + Bar + " has Value: " + Value;
   }
}

So now if you create an array of Foo objects named fooArray, typing ? fooArray in the Immediate window will list all the Foo objects with the ToString() return value for each in curly braces. Something like this:

所以现在如果你创建一个名为 fooArray 的 Foo 对象数组,输入 ? 立即窗口中的 fooArray 将列出所有带有 ToString() 返回值的 Foo 对象,每个对象都用花括号括起来。像这样的东西:

? fooArray
{Foo[2]}
[0]: {Bar: hi has Value: 1}
[1]: {Bar: there has Value: 2}

回答by Mike Kelly

I had this problem with the byte array contained within a MemoryStream - I found this worked to view the contents of the MemoryStream in the Visual Studio 2010 Watch window:

我在 MemoryStream 中包含的字节数组遇到了这个问题 - 我发现这可以在 Visual Studio 2010 Watch 窗口中查看 MemoryStream 的内容:

System.Text.ASCIIEncoding.ASCII.GetString(((((System.IO.MemoryStream)(s)))._buffer))

回答by Denise Skidmore

String.Join("; ", myArray);

回答by Ben Lesh

I use:

我用:

BitConverter.ToString(bytes); //output: 4A-0B-11-13  etc.

Poor BitConverter, always forgotten.

可怜的BitConverter,总是被遗忘。

回答by Technophile

Remember that you can reference a method in the program you are debugging. I have a utility method (let's say it's in a MyDebug class):

请记住,您可以在正在调试的程序中引用方法。我有一个实用方法(假设它在 MyDebug 类中):

    public static string ToReadableString(byte[] data)
    {
        int length = data.Length;
        var sb = new StringBuilder(length);
        for (int index = 0; index < length; ++index)
        {
            char ch = (char)data[index];
            sb.Append(Char.IsControl(ch) ? '.' : ch);
        }
        return sb.ToString();
    }

and (since my array is named data) add a Watch statement:

并且(因为我的数组名为 data)添加一个 Watch 语句:

MyDebug.ToReadableString(data)

回答by Ammar

You can always create a second variable with the content that you want and check for the value of that.

您始终可以使用所需的内容创建第二个变量并检查其值。

var ids = people.Select(s => s.id).ToList();

Would return all of the ids that you are trying to see.

将返回您尝试查看的所有 ID。

回答by Mano-Wii

If you want print for example a array mof type float[4][4]just type:(float(*)[4][4])m

如果你想打印例如一个m类型的数组,float[4][4]只需输入:(float(*)[4][4])m