One of my senior told me to use try/ finally block for all the methods to clear the initialized objects data.
for eg:
var serviceProxy = new NotificationServiceProxy();
try
{
return serviceProxy.GetNotifications(userID, request.Filters, request.FilterProperty, usertypeid);
}
finally
{
serviceProxy = null;
}
Is that a good practice? if i'm using try/ catch for all my methods to clear the initialized objects data.
Not in C#, use
usinginstead:This is the pattern to ensure that
IDisposablevariable get cleaned up.For instances that aren't disposable this isn't needed - just let them pass out of scope and leave it to the GC.
If
NotificationServiceProxyuses lots of resources and you need to be sure that it is finalised correctly then make it disposable and always wrap it in ausingor in another class that also implementsIDisposable. If it doesn't then thistry-finallypattern is a waste of time.