question

Atmapuri-5752 avatar image
0 Votes"
Atmapuri-5752 asked Atmapuri-5752 commented

C# 8.0 "using" bug?

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


dotnet-csharp
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.


If possible, show an example of MyObj class to reproduce the error.

0 Votes 0 ·

Dear Daniel,

This discussion is from another time. It was done before ".NET core". Yes, the time is passing. Please read this link from your boss:

https://devblogs.microsoft.com/dotnet/announcing-net-6-preview-1/

Where he says:
Hardware-accelerating structs

As a (stark) contrast to discussion on stackoverflow. Many things in .NET core have driven over many stubborn C# language issues, which persisted for decades.

Kind Regards!
Atmapuri






0 Votes 0 ·
Atmapuri-5752 avatar image
0 Votes"
Atmapuri-5752 answered Atmapuri-5752 edited

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

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Viorel-1 avatar image
0 Votes"
Viorel-1 answered

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.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Atmapuri-5752 avatar image
0 Votes"
Atmapuri-5752 answered DanielZhang-MSFT commented

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

· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi @Atmapuri-5752,
As offical document said that within the using block, the object is read-only and can't be modified or reassigned.
Best Regards,
Daniel Zhang


0 Votes 0 ·

Dear Daniel,

the object is read-only and can't be modified or reassigned.

That is not disputed or questioned. The "object" is read-only, but not for example, the file or the memory stream, to which it is written to. This does not imply that methods cant modify fields, which in fact they do. If methods can modify fields, why not have write access to "public" fields and properties directly? The current implementation only requires extra code and adds to the clutter, but does not actually prevent anything.

Atmapuri

0 Votes 0 ·

Hi @Atmapuri-5752,
On this problem, you can refer to more discussion here.
Best Regards,
Daniel Zhang


0 Votes 0 ·