private void test()
{
string tst = null;
try
{
tst = "abc";
}
catch (Exception)
{
throw;
}
finally
开发者_运维知识库{
tst = null;
}
}
My query is - Is it meaningful to type tst = null;
in the finally block as the string class doesn't have the disposable method ?
It doesn't have any effect, as tst
goes out of scope immediately after the finally
.
And this has nothing to do with Dispose
.
No this is not necessary. The variable tst
does no longer exists, when test()
returns.
Even if string
implemented IDisposable
, setting a variable to null wouldn't call the Dispose
method. It's utterly pointless, as is the catch block with the throw.
If you see code like this in a codebase you're maintaining, rip it out and have a quiet word with whoever put it in there in the first place.
精彩评论