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

后端 未结 10 1517
谎友^
谎友^ 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:55

    It's a by-product of a the parser rule that statement is either a simple statement or a block. i.e. a block can be used wherever a single statement can.

    e.g.

    if (someCondition)
     SimpleStatement();
    
    if (SomeCondition)
    {
       BlockOfStatements();
    }
    

    Others have pointed out that variable declarations are in scope until the end of the containing block. It's good for temporary vars to have a short scope, but I've never had to use a block on it's own to limit the scope of a variable. Sometimes you use a block underneath a "using" statement for that.

    So generally it's not valuable.

    0 讨论(0)
  • 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;
    }
    
    0 讨论(0)
  • 2021-01-11 21:05

    One reason for doing this is that the variables 'z' and 'zz' would not be available to code below the end of that inner block. When you do this in Java, the JVM pushes a stack frame for the inner code, and those values can live on the stack. When the code exits the block, the stack frame is popped and those values go away. Depending on the types involved, this can save you from having to use heap and/or garbage collection.

    0 讨论(0)
  • 2021-01-11 21:11

    This allows you to create a scope block anywhere. It's not that useful on it's own, but can make logic simpler:

    switch( value )
    {
        case const1: 
            int i = GetValueSomeHow();
            //do something
            return i.ToString();
    
        case const2:
            int i = GetADifferentValue();
            //this will throw an exception - i is already declared
     ...
    

    In C# we can use a scope block so that items declared under each case are only in scope in that case:

    switch( value )
    {
        case const1: 
        {
            int i = GetValueSomeHow();
            //do something
            return i.ToString();
        }
    
        case const2:
        {
            int i = GetADifferentValue();
            //no exception now
            return SomeFunctionOfInt( i );
        }
     ...
    

    This can also work for gotos and labels, not that you often use them in C#.

    0 讨论(0)
提交回复
热议问题