简体   繁体   中英

Accessing Properties of Base class from derived class using Generic List

In C#, was is the best way to access a property of the Base class when the generic list contains derived class.

public BaseClass1
{
  public Property A{get;set;}
   public Property B{get;set;}
}
 public BaseClass2:BaseClass1
{
   public Property C{get;set;}
   public Property D{get;set;}
}
public classA:BaseClass2
{
}

public class Implement
  {
    List<classA> list1 = new List<classA>()

    Console.WriteLine("Would you like to add another person");//If yes I would like add person to the list dynamically

    {
        //Is it possible to acesses properties of Baseclass using derived class List
       List1.Property A = Console.readline();//read input from console
       List1.Property B = Console.readline();//read input from console
           .
           .
       List.Property D = Console.readline();//read input from console

       List1.add(Property A,Property B);//add properties of baseclass1 and baseclass2 as well as derived class 

    }
   }

I want to take values of base class properties from the console and add those values to the List and increase the list if the user wants to add more objects of classA. Is this possible??

List1.Add( new ClassA { A = a, B = b, C = c } );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM