I have the following click event.
protected void btnUpdate_Click(object sender, EventArgs e) {
foreach (GridViewRow gvr in gvEditBulletins.Rows) {
RadEditor re = (RadEditor)gvr.FindControl("reBulletin");
DropDownList ddl = (DropDownList)gvr.FindControl("ddlPosition");
// Business logic
}
}
Do I suffer a performance hit since I’m declaring an instance of the RadEditor and DropDownList with every iteration, or is the compiler smart enough to know to reuse the instances?
First, even worrying about this is a micro-optimization; you shouldn’t worry about it until you are studying the performance of your application and profiling suggests that this method and this loop are bottlenecks. Second, yes the compiler will reuse the same instance of the local variables
reandddl.Here’s a very simple example:
Here’s the IL:
}
Notice that in the locals section, the variable
iis declared as occupying the second location on the stack and this is where the result ofget_Lengthis repeatedly being stored on each iteration through the loop. (I have highlighted the relevant lines with--->s in the margin.)