badgerbadger
badgerbadger

Reputation: 323

C#, creating a list of bank account objects

New to C# coming over from C++. I have an assignment where I have an Account parent class and a Current Account and Deposit Account derived classes. Desposit account also has an SSIA account class derived from it. In main, I want something I can store objects of all these types in. In C++, I used a vector. Could I use list in C#? How would I do it so I can store all these different kinds of objects in it?

Thanks for any help!

Upvotes: 0

Views: 1275

Answers (1)

user2321864
user2321864

Reputation: 2307

List<T> or any other generic collection class can store object of Account and any object derived from the Account class

public class Account {
}
public class DepositAccount : Account {
}

var accounts = new List<Account>();
accounts.Add(new Account());
accounts.Add(new DepositAccount()); //assuming DepositAccount is derived from Account

var depositA = new DepositAccount();
accounts.Add(depositA);

Upvotes: 1

Related Questions