Why is no warning given for this unused variable?

前端 未结 7 547
-上瘾入骨i
-上瘾入骨i 2020-12-06 16:25

When compiling the following program in VS2010, VS2008 or MonoDevelop on Windows, I get warning CS0219, \"The variable \'y\' is assigned but its value is never used\".

相关标签:
7条回答
  • 2020-12-06 16:59

    It could be that since x is a reference type, and is thus stored on the heap, that it would prevent garbage collection of that object until x goes out of scope.

    For example:

    void main(string[] args)
    {
        object x = new object();
        while (true)
        {
            // Some threading stuff
            // x is never garbage collected
        }
    }
    

    In contrast to:

    void main(string[] args)
    {
        new object();
        while (true)
        {
            // Some threading stuff
            // The unreferenced object IS garbage collected
        }
    }
    
    0 讨论(0)
提交回复
热议问题