Мне нужна помощь в создании и уничтожении объектов или жизненного цикла объектов. Проблема заключается в том, что после удаления формы временные объекты не стираются и отношения с объектом все еще существуют. Что я делаю неправильно? Нужно ли мне переопределить метод или деструктор? Как правильно уничтожить временный объект? Благодарю.Как правильно уничтожить временный объект C#
Форма выпуска:
private Product T = null;
public ProductForm(Customer cust)
{
InitializeComponent();
T = new Product();
T.Customer = cust;
}
Public CopyProducts(List<Product> copy_from)
{
Foreach(Product Temp1 in copy_from)
{
Product Temp2 = new Product();
Temp2.Customer = T.Customer;
Temp2.Count = Temp1.Count;
Temp2.Delivery = Temp1. Delivery;
…
cDB.Product.InsertOnSubmit(Temp2);
cDB.SubmitChanges();
}
}
private void OKButton_Click(object sender, EventArgs e)
{
T.Count = …
…
cDB.Product.InsertOnSubmit(T);
cDB.SubmitChanges();
}
private void CancelButton_Click(object sender, EventArgs e)
{
this.Close();
}
Тест-код в основной форме:
MessageBox.Show(current_customer.Product.Count().ToString()); // ->>> 10
ProductForm prodform = new ProductForm(current_customer);
prodform.ShowDialog();
prodform.Dispose();
// ->>> after cancel button clicked should be 10 but it shouws 11
MessageBox.Show(current_customer.Product.Count().ToString()); // ->>> 11 instead of 10
и
MessageBox.Show(current_customer.Product.Count().ToString()); // ->>> 10
ProductForm prodform = new ProductForm(current_customer);
prodform.CopyProducts(list_of_products);
prodform.Dispose();
MessageBox.Show(current_customer.Product.Count().ToString()); // ->>> 11 instead of 10
Что это связано с Linq? – DavidG
Вам не нужно его утилизировать, пусть сборщик мусора выполняет свою работу. –
Но это не так. Пример кода показывает это. Почему я спрашиваю. – John4