开发者

string class disposable method

开发者 https://www.devze.com 2023-04-01 04:15 出处:网络
private void test() { string tst = null; try { tst = \"abc\"; } catch (Exception) { throw; } finally 开发者_运维知识库{
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.

0

精彩评论

暂无评论...
验证码 换一张
取 消