C
C#17mo 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
moshimoshi17mo ago
E.g., why do we have to use property function if field is set to public?
Pobiega
Pobiega17mo ago
You only have one field here, and its private. you have one property, and its public
moshimoshi
moshimoshi17mo 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
Pobiega17mo 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
moshimoshi17mo ago
oh my bad! i just realized
Pobiega
Pobiega17mo ago
is a constructor
moshimoshi
moshimoshi17mo ago
can I ask, why dont the account field go into the constructor's args?
Pobiega
Pobiega17mo 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
Anton17mo 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
Accord17mo 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
More Posts