-2
public class A
{
    public long TestCaseId { get; set; }
    public byte TestCaseNumber { get; set; }

    public object FetchList { get; set; }
} 
class Program
{
    static void Main(string[] args)
    {
        List<int> a = new List<int>(){ 1, 2, 3, 4 };
        List<A> Objectlst = new List<A>();
        Objectlst .Add(new A { TestCaseId = 1, TestCaseNumber = 1, FetchList = a });
        foreach (var item in Objectlst)
        {
            Console.WriteLine(item.FetchList);
        }
    }
}

There is a list of class A. If I am assigning a generic list to Object datatype FetchList, Can someone tell me how to retrieve all the values stored in list through this property FetchList?

Aristos
  • 66,005
  • 16
  • 114
  • 150

1 Answers1

0
foreach (var item in Objectlst)
{
    var list = item.FetchList as List<int>;
    if (list != null)
        foreach (int num in list)
            Console.WriteLine(num);
}
Zer0
  • 7,191
  • 1
  • 20
  • 34
  • thanks a lot! but what if I don't know the type of list example, in here you used as List but what if you don't know the type of list i.e it is not generic some random class type. – Siddharth Dwivedi Apr 10 '20 at 08:51
  • @SiddharthDwivedi Then you shouldn't be using `object`. Sure, you can discover type information at runtime with reflection, but C# is a strongly-typed language. So use compile time type safety is my advice. – Zer0 Apr 10 '20 at 10:09