1
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();
        }
    }
}

The output is : output from console

But i want this output :

this isit

What For

for it

So what did i do wrong and what to consider in the future ?

Bioukh
  • 1,888
  • 1
  • 16
  • 27
  • This question is not relative to Arrays, but about the behavior of Object.ToString() function. – Bioukh Nov 24 '19 at 09:41

3 Answers3

3

You must add the following function in your class Indix :

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

By default, ToString() returns the name of the object class.

Bioukh
  • 1,888
  • 1
  • 16
  • 27
2

your code is correct you just have to accesss properties of class like below:

foreach (Indix v in m)
{
    Console.WriteLine(v.i0 +" "+v.x0);
}
Simply Ged
  • 8,250
  • 11
  • 32
  • 40
Usman Asif
  • 320
  • 2
  • 12
2

You need to overwrite the ToString() method in your Indix Class.

For example:

public override string ToString()
{
    return $"{i0} {x0}";
}
Malte
  • 301
  • 2
  • 3