Lijin John Lijin John - 2 months ago 11
C# Question

Not able to retrieve methods once added in List<object> in c#

I am having a hard time understanding inheritance in C#. Suppose, i have two classes like this

public class Base
{
public void print()
{
Console.WriteLine("this is a BASE method");
}
}
public class Derived : Base
{
public void print()
{
Console.WriteLine("this is a DERIVED method");
}
}



now when i create a
List<object>
and try to add the two objects
like this and try to access the method, i am not able to do so


class Program
{
static void Main()
{

List<object> lists = new List<object>();
lists.Add(new Base());
lists.Add(new Derived());
foreach (var a in lists)
{

}
Console.ReadLine();
}
}


but when i add two integer value and try to access it, i am able to access it, how is this possible?

static void Main()
{
List<object> lists = new List<object>();
lists.Add(4);
lists.Add(5);
foreach (var a in lists)
{
Console.WriteLine(a);
}
Console.ReadLine();
}


How am i able to get the integer value without even converting it, am i missing some key concept here, please help me understand

UPDATE 1

well let me try to explain my question one more time, when i want to call a method from the added lists i have to convert it to the respective type like this

A a = lists[0] as A;
B b = lists[1] as B;
C c = lists[2] as C;
a.print();
b.print();
c.print();


but when i add int to the list like this

List<object> lists = new List<object>();
lists.Add(4);
lists.Add(5);
foreach (var a in lists)
{
Console.WriteLine(a);
}


why is that there is no need to convert it first into int and then print it

Answer

The key concept you are missing is .ToString() is overridden in int which makes it print the number instead of System.Int32. You can make your code do it too by simply adding a public override string ToString() to your functions.

public class Base
{
    public override string ToString()
    {
        return "this is a BASE method";
    }
}

public class Derived : Base
{
    public override string ToString()
    {
        return "this is a DERIVED method";
    }
}

class Program
{
    static void Main()
    {

        List<object> lists = new List<object>();
        lists.Add(new Base());
        lists.Add(new Derived());
        foreach (var a in lists)
        {
            Console.WriteLine(a);
        }
        Console.ReadLine();
    }
}

To actually be able to call print() you need to make your list of type Base instead of object and you need to mark the print function as virtual in base and override in the derived

public class Base
{
    public virtual void print()
    {
        Console.WriteLine("this is a BASE method");
    }
}

public class Derived : Base
{
    public override void print()
    {
        Console.WriteLine("this is a DERIVED method");
    }
}

static void Main()
{

    List<Base> lists = new List<Base>();
    lists.Add(new Base());
    lists.Add(new Derived());
    foreach (var a in lists)
    {
        a.print();
    }
    Console.ReadLine();
}