0

I use the classes Form1.cs,MockProduct.cs and Product.cs.
To add a new entry, I use the method:

public partial class Form1: Form     {         public Form1 ()         {             InitializeComponent ();         }                public void AddItem ()         {             Product product = new Product ()             {                 ID = 4,                 Name = "Name_4",                 Description = "Description_4"             };               MockProduct.ProductList.Add (product);               var v = MockProduct.ProductList;         }       } 

 
I am checking a record using the expression MockProduct.ProductList.Count.
Result: MockProduct.ProductList.Count = 3.
In other words, the entry is not added.
 
Question.
1. How to add an element to a property of type List ?
2. Does my code have an error?
 


Picture-1
enter image description here

MockProduct.cs

static class MockProduct { static List<Product> productList; public static List<Product> ProductList { get { return productList = new List<Product> { new Product {ID = 1, Name = "Name_1", Description = "Description_1"}, new Product {ID = 2, Name = "Name_2", Description = "Description_2"}, new Product {ID = 3, Name = "Name_3", Description = "Description_3"}, }; } set { productList = value; } } } 

Product.cs

public class Product { public int ID { get; set; } public string Name { get; set; } public string Description { get; set; } } 
1
  • 4
    That's because every time you invoke the getter you create a new list and return it. Commented Apr 19, 2020 at 17:50

3 Answers 3

2

Every time, when you call getter of ProductList property, you'll create and initialize a new instance of productList field (using new operator), therefore you always getting ProductList.Count equals 3. Try to move this logic outside the property and initialize productList only once, when declaring it

static List<Product> productList = new List<Product> { new Product {ID = 1, Name = "Name_1", Description = "Description_1"}, new Product {ID = 2, Name = "Name_2", Description = "Description_2"}, new Product {ID = 3, Name = "Name_3", Description = "Description_3"}, }; public static List<Product> ProductList { get { return productList; } set { productList = value; } } 
Sign up to request clarification or add additional context in comments.

Comments

1

If you want a initial value for your list you could instantiate your list only the first time you access it:

get { if (productList == null) { productList = new List<Product> { new Product {ID = 1, Name = "Name_1", Description = "Description_1"}, new Product {ID = 2, Name = "Name_2", Description = "Description_2"}, new Product {ID = 3, Name = "Name_3", Description = "Description_3"}, }; } return productList; } set { productList = value; } 

Comments

0
  1. How to add an element to a property of type List ?

You are using the right method:

ProductList.Add (product); 
  1. Does my code have an error?

Yes, the getter for ProductList returns always a new list. Remove this code from MockProduct class:

 public static List<Product> ProductList { get { return productList = new List<Product> { new Product {ID = 1, Name = "Name_1", Description = "Description_1"}, new Product {ID = 2, Name = "Name_2", Description = "Description_2"}, new Product {ID = 3, Name = "Name_3", Description = "Description_3"}, }; } set { productList = value; } } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.