L
L
lipuha32022-03-08 09:37:15
C++ / C#
lipuha3, 2022-03-08 09:37:15

How to add new values ​​to a property?

Let's say I have a project "online grocery store", and in this project there is a class with the store itself. Logically, this store stores products. And for this, a separate class "Product" is implemented. How can I implement the "Product" class in such a way that it would store the product code, the product name, and the price of the product, and then display all this information?

class Magazine
{
    public string Product { get; set; }
}

class Product
{

}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Morrowind, 2022-03-12
@hekkaaa

Hey!
For the property you specified, this is:

Magazine item = new Magazine();
item.Product  = "Hook";

Also note that when creating properties, it is possible to do init (or you may encounter this).
Then this will only work at the time of class initialization:
class Magazine
{
    public string Product { get; init; }
}
Magazine item = new Magazine(){ Product = "Soda" };

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question