ABM ABM - 17 days ago 6
C# Question

How to pass data from class using a list

namespace ConsoleApplication3
{
class A
{
public int a = 100;
}
class Program
{
static void Main(string[] args)
{
ArrayList list = new ArrayList();
A a = new A() ;
list.Add(a);
foreach (var i in list)
{
Console.WriteLine(i);
}
Console.ReadKey();

}
}
}


this code gives the output:

ConsoleApplication3.A


How can I get the value from the class
A
without using
.a
and
this
? I want to continue to achieve this using
ArrayList
.

Answer

You can override ToString() in class A.

public class A
{
    public int a = 100;

    public override string ToString()
    {
        return a.ToString();
    }
}
Comments