Suppose there is a designer class "Bank". Here we need to transfer the name, passport number and number of customer accounts, as well as deposits on each account. How to transfer the list of contributions to the designer?

public Bank(string name, string passport, int colSchet, ) { } 

2 answers 2

 public Bank(string name, string passport, int colSchet, IReadOnlyList<Deposit> deposits) { } 

So you can pass to the designer any collection that implements this interface, including arrays ( Deposit[] ) and sheets ( List<Deposit> ). If you need Bank to modify the collection, use the IList<Deposit> interface.

In general, the logic is not clear. The constructor is used to initialize the object. Is the bank dependent on one particular customer? I think you need to replace Bank with Client.

  • one
    Then IEnumerable is better right away (if the constructor does not need to be addressed by the index, of course). - andreycha

The easiest, probably, way is to add one more parameter to the constructor and you get something like this:

 public Bank( string name, string passport, int colSchet, IEnumerable<Contribution> contributions) { // код Вашего конструктора } 

But, if you do not want to add a new parameter to the constructor, you can do a little differently, during initialization, create or assign a collection:

 var bank = new Bank("name", "passport", 1) { Contributions = new List<Contribution>() // я решил создать { new Contribution(1), new Contribution(2), new Contribution(3) } }; 

And then in debugging everything is clear that everything is in order: enter image description here

Only the Contributions property must be public . This is purely formal, for example, if you have a Contribution class, which in your class is a list, well, in a sense, if so:

 public class Bank { public string Name { get; set; } public string Passport { get; set; } public int ColSchet { get; set; } public List<Contribution> Contributions { get; set; } public Bank(string name, string passport, int colSchet) { Name = name; Passport = passport; ColSchet = colSchet; } } 

Well, somewhere there is a class Contribution course:

 public class Contribution { public int Value { get; set; } public Contribution() { } public Contribution(int value) { Value = value; } }