我正在開發一個控制臺應用程式,在該應用程式中,我通過用戶輸入將成員添加到串列中。使用的邏輯是:
public class Projectlogic
{
public List<Project> Projects { get; set; } = new List<Project>();
//Add a project to the list Projects.
public void Add(Project project)
{
Projects.Add(project);
}
public List<Project> GetAllProjects()
{
return Projects;
}
}
下面給出了將專案添加到專案串列中的主程式。當我除錯代碼時,專案中的計數顯示為 0。我遺漏了哪一部分邏輯或使用不正確?
//Adding parameters PID, Name, Sdate, Edate to a project.
var newProject = new Project(PID, Name, Sdate, Edate);
var business2 = new ProjectLogic(); //declared
business2.Add(newProject);
break;
uj5u.com熱心網友回復:
請參閱我在右側的評論(縮短變數名稱以創建更多空間):
var newProject = new Project(PID, Name, Sdate, Edate);
var b2 = new ProjectLogic(); //A new object with 0 items is created.
//A breakpoint here will ALWAYS show zero items
b2.Add(newProject); //Add the item, but the change won't show until the next line.
//Breakpoints here will also ALWAYS show 0 items.
break; //A breakpoint here will ALWAYS show ONE item.
//However, "break" will kick you out of the current scope.
//This means the business2 object also immediately goes
//out of scope and is thrown away.
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/425688.html
標籤:C#
上一篇:MicrosoftIDisposable模式真的正確嗎?
下一篇:在C#中鏈接多載的建構式
