Monday, 9 September 2013

Is this a valid object disposal code pattern in method with return?

Is this a valid object disposal code pattern in method with return?

I noticed the following object disposal code pattern in a C# project and I
was wondering if it's acceptable (although it works).
public object GetData()
{
object obj;
try
{
obj = new Object();
// code to populate SortedList
return obj;
}
catch
{
return null;
}
finally
{
if (obj != null)
{
obj.Dispose();
obj = null;
}
}
}
For this example, I'm using a general 'object' instead of the actual
IDisposable class in the project.
I know that the 'finally' block will be executed every time, even when the
value is returned, but would it affect the return value (or would it be a
new object instance) in any way since the object is being set to null (for
what seems like object disposal and GC purposes).

No comments:

Post a Comment