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?
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; } } 