Question about C # Collective Collection

The following code is the Type of our Dictionary <int, Customer>, but how do you know what type of Client is? It seems that the Client is the line here, since we are the client cust1 = new client (1, "Cust 1"); .... im confused ...

 public class Customer
    {
        public Customer(int id, string name)
        {
            ID = id;
            Name = name;
        }

    private int m_id;

    public int ID
    {
        get { return m_id; }
        set { m_id = value; }
    }

    private string m_name;

    public string Name
    {
        get { return m_name; }
        set { m_name = value; }
    }
}

 Dictionary<int, Customer> customers = new Dictionary<int, Customer>();

Customer cust1 = new Customer(1, "Cust 1");
Customer cust2 = new Customer(2, "Cust 2");
Customer cust3 = new Customer(3, "Cust 3");

customers.Add(cust1.ID, cust1);
customers.Add(cust2.ID, cust2);
customers.Add(cust3.ID, cust3);

foreach (KeyValuePair<int, Customer> custKeyVal in customers)
{
    Console.WriteLine(
        "Customer ID: {0}, Name: {1}",
        custKeyVal.Key,
        custKeyVal.Value.Name);
}
+3
source share
3 answers

A Customerobject is a type Customer, although it consists of intand a string. When you call Customer cust1 = new Customer(1, "Cust 1");that really says "make me an object of type Customerthat consists of the integer 1 and the string Cust 1"

+8
source

Customer - Customer. A class - , ( - struct).

, , - , . .

+6

Your customer representation in this example is just an identifier and a name.

You can imagine it with many things being the identifier and the name you have chosen for this occasion.

The above example is just a constructor call in which you provide the identifier and name of this particular client.

0
source

Source: https://habr.com/ru/post/1714159/


All Articles