C
C#2y ago
moshimoshi

❔ Polymorphism

Can someone help me understand this part of the code? I dont understand why its being written like this...
public class BankBranch
{
//
// This field keep a list of bank accounts.
// Any type of accounts will be accepted
//
private List<Account> _accounts;

public BankBranch(string name)
{
Name = name;
_accounts = new List<Account>();
}

// Auto-properties
public string Name { get; set; }
public class BankBranch
{
//
// This field keep a list of bank accounts.
// Any type of accounts will be accepted
//
private List<Account> _accounts;

public BankBranch(string name)
{
Name = name;
_accounts = new List<Account>();
}

// Auto-properties
public string Name { get; set; }
10 Replies
moshimoshi
moshimoshiOP2y ago
E.g., why do we have to use property function if field is set to public?
Pobiega
Pobiega2y ago
You only have one field here, and its private. you have one property, and its public
moshimoshi
moshimoshiOP2y ago
isnt these the fields
private List<Account> _accounts;
public BankBranch(string name)
{
Name = name;
_accounts = new List<Account>();
}
private List<Account> _accounts;
public BankBranch(string name)
{
Name = name;
_accounts = new List<Account>();
}
and this the property?
public string Name { get; set; }
public string Name { get; set; }
Pobiega
Pobiega2y ago
private List<Account> _accounts; is a field
public BankBranch(string name)
{
Name = name;
_accounts = new List<Account>();
}
public BankBranch(string name)
{
Name = name;
_accounts = new List<Account>();
}
moshimoshi
moshimoshiOP2y ago
oh my bad! i just realized
Pobiega
Pobiega2y ago
is a constructor
moshimoshi
moshimoshiOP2y ago
can I ask, why dont the account field go into the constructor's args?
Pobiega
Pobiega2y ago
It could! Depends on how you want it to work. In this case, it always creates a new, empty list when a branch is constructed.
Anton
Anton2y ago
you could do that, but this class is assumed to own the state, so giving it a mutable list could lead to unexpected things
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server