c# - 对象数组未按预期交付

标签 c# arrays

<分区>

using System;
namespace TestArr
{
    class Indix
    {
        public string i0 = null;
        public string x0 = null;
        public Indix(string i1, string x1)
        {
            i0 = i1;
            x0 = x1;
        }
    }
    class mainex
    {
        public mainex()
        {
            Indix[] m = new Indix[3];
            m[0] = new Indix("this", "isit");
            m[1] = new Indix("What", "For");
            m[2] = new Indix("for", "it");
            foreach (Indix v in m)
            {
                Console.WriteLine(v.ToString());
            }
            Console.ReadKey();
        }
        static void Main()
        {
            mainex h = new mainex();
        }
    }
}

输出是:output from console

但我想要这个输出:

this isit

What For

for it

那么我做错了什么以及以后要考虑什么?

最佳答案

您必须在类 Indix 中添加以下函数:

public override string ToString()
{
    return io + " " + xo;
}

默认情况下,ToString() 返回对象类的名称。

关于c# - 对象数组未按预期交付,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59015983/

相关文章:

c# - Process.Start 文件名使用 %temp%

c# - 多选列表框asp mvc

c# - 用 C# 属性替换 XML 注释的正则表达式

c# - IEnumerable 到参数数组

java - 将频率应用于数组中的元素

javascript - 从数组中删除特定项目,未按预期工作

java - 创建字节数组并获得相同的 Java 和 JavaScript 输出

c# - Web 服务不返回当前 Windows 用户名

数组上的Javascript编程

c# - ptr[i] 和 *(ptr + i) 有什么区别?