Why doesn't the parent object crash with the previously installed child?

A potentially embarrassing question, but obviously something is missing for me what I want / need to know.

I expect the following code to create a new row in the table with new cells to be displayed later. And this is what he does ... as one would expect.

using (TableRow tr = new TableRow())
{
    using (TableCell td = new TableCell())
    {
        td.Text = "Column A";
        tr.Cells.Add(td);
    }
    using (TableCell td = new TableCell())
    {
        td.Text = "Column B";
        tr.Cells.Add(td);
    }
    tbl.Rows.Add(tr);
}

But .... but are not TDs created in use operations invalid after they leave the scope of use? Will the TD objects referenced by the string be invalid, and should the string fail when it tries to use them? The same can be said for TR when it is displayed by the 'tbl' object.

Don't you understand to dispose of?

Do I not understand the use?

TableRow.Cells.Add() , ptr?

TableCell ?

?

+3
1

"using" , "Dispose" . - , "", , - , .

"Dispose" TableRow TableCell , . .

, , "" . .

Edit: "Dispose" . , , .Net . Reflector .

at TestControl.Dispose() in D:\TestControl.cs:line 25  
at System.Web.UI.Control.UnloadRecursive(Boolean dispose)   
at System.Web.UI.Control.UnloadRecursive(Boolean dispose)   
at System.Web.UI.Control.UnloadRecursive(Boolean dispose)  
at System.Web.UI.Control.UnloadRecursive(Boolean dispose) 
at System.Web.UI.Page.UnloadRecursive(Boolean dispose)  
at System.Web.UI.Page.ProcessRequestCleanup()   
at System.Web.UI.Page.ProcessRequest(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)   
at System.Web.UI.Page.ProcessRequest()   
at System.Web.UI.Page.ProcessRequestWithNoAssert(HttpContext context)   
at System.Web.UI.Page.ProcessRequest(HttpContext context)
+6

Source: https://habr.com/ru/post/1716502/


All Articles