What is the value of an anonymous unattached block in C#?

后端 未结 10 1590
谎友^
谎友^ 2021-01-11 20:43

In C# you can make a block inside of a method that is not attached to any other statement.

    public void TestMethod()
    {
        {
            string x          


        
10条回答
  •  没有蜡笔的小新
    2021-01-11 20:59

    There is no value to this other than semantic and for scope and garbage collection, none of which is significant in this limited example. If you think it makes the code clearer, for yourself and/or others, then you certainly could use it. However, the more accepted convention for semantic clarification in code generally would use line breaks only with option in-line comments:

    public void TestMethod()
    {
        //do something with some strings
        string x = "test";
        string y = x;
    
        //do something else with some ints
        int z = 42;
        int zz = z;
    }
    

提交回复
热议问题