我看到很多的代码,其中所述对象被设置为空的finally
部分或在方法结束时,例如:为什么使用后应该将对象设置为null是否有原因?
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public void buildUpdates(int batchId, string mkt)
{
aplUpdate oDU;
DataSet ds;
DataTable dtTbls;
DataTable dtData;
List<dbVer> resp;
try
{
oDU = new WEB_APL.APL.aplUpdate(MyWeb.Properties.Settings.Default.DBCon);
ds = new DataSet();
ds.DataSetName = batchId.ToString();
dtTbls = oDU.getDetails(true);
resp=GetDBVersions();
foreach (DataRow dr in dtTbls.Rows)
{
.....
}
ds.WriteXml(HttpContext.Current.Server.MapPath("~") + "\\" + ds.DataSetName + ".xml", XmlWriteMode.WriteSchema);
}
catch (Exception ex)
{
throw ex;
}
finally
{
oDU = null;
ds=null;
rest=null;
dtTbls=null;
dtData=null;
}
}
我认为这是unnessesary因为过期的对象将被自动处理.NET garbage collector
。
是否有理由重置为null
?
[吗?您需要处置的对象,并将它们设置为null(HTTP的可能重复://计算器。com/questions/2926869/do-you-need-to-dispose-of-objects-and-set-them-to-null)或[在.NET中使用后将对象设置为Null/Nothing](http:// stackoverflow .com/questions/2785/setting-objects-null-null-nothing-in-net) –