C# 8.0 "using" bug?

Atmapuri 81 Reputation points
2021-04-03T15:22:15.507+00:00

Hello,

When declaring:

using anObj = new MyObj();

it is normal and correct to indicate an error, if anObj, is to be overwritten:

anObj = bnObj; //error (correct)

but, it makes no sense to block assignments to indexed property:

anObj[i] = aValue; //also error (but unecessary)

This is by far not the same as overwriting the object. Tested with latest compiler. Can this be expected to be fixed?

Thanks!
Atmapuri

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,993 questions
{count} votes

3 answers

Sort by: Most helpful
  1. Atmapuri 81 Reputation points
    2021-04-03T17:28:58.957+00:00

    Dear Viorel,

    Please see here:

        public struct aMyStruct: IDisposable
        {
            int a;
            public void Dispose()
            {
                //
            }
    
            public int this[int i]
            {
                get
                {
                    return a + i;
                }
                set
                {
                    a = value - i;
                }
            }
        }
    
        private void button1_Click(object sender, EventArgs e)
        {
            using aMyStruct anObj = new aMyStruct();
            anObj[1] = 2;  //Cannot modify members of 'anObj' because it is a 'using variable'
        }
    

    And it is not only about indexed property, but all members actually. Even cannot read/write an int field.

    Atmapuri

    0 comments No comments

  2. Viorel 117.9K Reputation points
    2021-04-03T20:10:35.203+00:00

    If you use class instead of struct, or use functions instead of properties, then it works: you can modify the item. But you still cannot modify the variable that was declared in using statement. I think that it is a designed limitation of using statement, not a defect.

    0 comments No comments

  3. Atmapuri 81 Reputation points
    2021-04-04T05:42:20.38+00:00

    Dear Viorel,

    If you use class instead of struct, or use functions instead of properties, then it works

    Can you put forth any possible semi-reasonable explanation, why this would make sense: to require the use of functions or to declare a class rather than a struct to be able to modify fields, without actually preventing their modification? And why would a struct require the use of more code to achieve the same in compare to a class? Or why would "using" which is designed to implement try/finally/Dispose(), meddle with write access modifiers on a struct, but not on a class?

    This looks like a quick patch on the compiler level, to fix a designer bug in the specific struct type implementation of the .NET framework. It makes no sense for the language as such. And here is the reason for the use of struct over classes: because they don't allocate memory on the GC on heap on their own. On one side, there are a bunch of classes, like Span<T>, t avoid the need for memory allocation and on the other side, it is encouraged (by requiring classes over structs?)

    Atmapuri


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.