18

I wonder why does it not compile?

public static void Main(string[] args)
{
    using (MyStruct sss = new MyStruct())
    {

        sss.s = "fsdfd";// Cannot modify members of 'sss' because it is a 'using variable' 

        //sss.Set(12);    //but it's ok
    }
}

public struct MyStruct : IDisposable
{
    public int n;
    public string s;

    public void Set(int n)
    {
        this.n = n;
    }
    public void Dispose()
    {
        Console.WriteLine("dispose");
    }
}

UPDATE: But it works perfect. Why?

public static void Main(string[] args)
        {

            using (MyClass sss = new MyClass())
            {
                sss.Field = "fsdfd"; 
            }


        }

public class MyClass:IDisposable {

    public string Property1 { get; set; }
    public string Field;
    public void Method1 (){}

    public void Dispose()
    {
        Console.WriteLine("dispose class");
    }
 }
9
  • 15
    I will shoot people if I ever saw anyone implementing IDisposable on a struct.
    – leppie
    Oct 27, 2011 at 10:14
  • 13
    @leppie: will that be random people or the person implementing it? Could be good to know. Oct 27, 2011 at 10:19
  • 3
    @leppie You should explain why :-) And I still don't see the explanation on the reason of this. Why with class(es) it's different?
    – xanatos
    Oct 27, 2011 at 10:24
  • As a sidenote, you can't do it even when you are using a foreach (var sss in new List<MyStruct>())
    – xanatos
    Oct 27, 2011 at 10:30
  • 4
    @leppie I found a quite legitimate use for struct : IDisposable is abusing using() to ensure XML/HTML markup elements being written with HtmlTextWriter are correctly closed (the struct's Dispose method calls WriteCloseTag with the correct tag-name - though my struct is immutable). It results in much more readable code that resembles the rendered HTML because the using is indented just like the element it's rendering. The C# compiler will not box the struct because the CIL uses ldloca.s+constrained+callvirt - but I was hoping it would use duck-typing like foreach does.
    – Dai
    Jun 25, 2020 at 6:43

8 Answers 8

33

A number of people have linked to my article about mutating value types and why it is a bad idea. Though it is important to understand those concepts when understanding why its a bad idea to dispose a struct, and a worse idea to mutate the struct when you do, that is not actually the right article to link to. The one you want to read that explains all this in excruciating detail is:

http://ericlippert.com/2011/03/14/to-box-or-not-to-box/

In short: "using" makes a copy of the value type, and you are therefore disposing a copy. That means you have to be very careful -- if the value is, say, an OS handle, there might be lots of copies of that value lying around memory, and you'll need to make sure that you dispose it exactly once no matter how many copies there are.

See also If my struct implements IDisposable will it be boxed when used in a using statement?

13
  • I'll readily grant that struct methods which alter "this" without "telling anyone" are evil (it's unfortunate that there's no means of tagging such methods so their use could be forbidden in read-only contexts). I disagree strenuously, however, with the notion that structs should not have public mutable fields; see stackoverflow.com/questions/441309/why-are-mutable-structs-evil for simple code examples. Consider these questions: How many distinct Point instances are in an Array<Point> of size 100? How many distinct Dictionary instances are in an Array<Dictionary> of size 100?
    – supercat
    Oct 27, 2011 at 19:43
  • 1
    (Dictionary was chosen as an arbitrary class type--I know it's not the best for that example). If someStructType has a public int field I, and StructArr is an array of someStructType, what effect will "StructArr[3].I+=4;" have on StructArr[4].I ? If someClassType has a public int field I, and ClassArr is an array of someClassType, what effect will "ClassArr[3].I+=4;" have on ClassArr[4].I? Classes certainly have their place, but if a type is used to represent values, and if mutability would be helpful, then a mutable value type is often more appropriate than a mutable class type.
    – supercat
    Oct 27, 2011 at 19:55
  • @supercat: I commented on the post you linked but in short: everything you argue for can be done with immutable value types. Why should they specifically be mutable? Oct 27, 2011 at 23:23
  • @Ron Warholic: With an immutable entity, one must rewrite all the fields if one wants to change any of them. If what one wants to do is change one field of a struct, I would suggest that doing so is both clearer and more performant than creating a new structure in which nearly all of the fields have values that happen to match those of the first. What possible advantage is gained by rewriting everything in order to make one change?
    – supercat
    Oct 28, 2011 at 15:17
  • @supercat: How exactly is mutating a value type clear when it is trivial to give examples where mutating a value type produces incorrect results? The reason people argue for immutability on value types is precisely because the performance loss (if any) is well worth the ability to reason about the code effectively. For the absolute most performance critical areas mutable value types can be useful (say for vertices in a 3D model); but those types of situations are few and far between. Oct 28, 2011 at 16:12
3

The class and struct scenarios are actually the same but you see different effects.

When you change the class example to :

using (MyClass sss = new MyClass())
{
    sss = null;          // the same error
    sss.Field = "fsdfd"; // ok
}

You will get the same error on the first assignment.

The explanation is: You cannot change (mutate) the using-variable. But for a class that applies to the reference, not to the instance.

And the lesson is: Don't use structs. And especially don't use mutable structs.

1
  • 2
    @Alex: something that is "mutable" is something that can change (mutate). A struct that can change its value is called a mutable struct, and is generally considered something bad. More reading: Why are mutable structs evil? Oct 27, 2011 at 12:00
3

The idea of a using statement is to ensure that a resource is disposed of when you leave the block.

When you assign to a struct variable, you are effectively replacing it with a completely new object of that type. So in this context you would have got rid of the object that was supposedly going to be disposed of.

5
  • To add, you will be disposing the unmanaged resources while still using another copy of the struct. KABOOOM!
    – leppie
    Oct 27, 2011 at 10:33
  • Are struct copy-on-write in C# when using fields/properties?
    – xanatos
    Oct 27, 2011 at 10:37
  • @xanatos: No, but the using (and foreach) constructs create a temporary variable. Classes are good since this is only a reference.
    – leppie
    Oct 27, 2011 at 10:40
  • @xanatos - no. Are you thinking about how strings are sometimes implements in C++? Structs are just mutable objects that are solely owned by the variable that refers to them. The variable may be immutable (as in this case), and the author of the struct may mark individual fields within the struct as immutable (via the readonly keyword). Oct 27, 2011 at 10:42
  • @DanielEarwicker And in fact the (real) question was/should be "why the variable is immutable" not "why I can't use fields". And it's interesting that you can't use even properties (considering that a property doesn't really NEED to modify a field).
    – xanatos
    Oct 27, 2011 at 10:45
2

Consider the following:

 interface IFoo: IDisposable { int Bar {get;set;}}

 struct Foo : IFoo
 {
   public int Bar { get; set; }
   public void Dispose() 
   {
     Console.WriteLine("Disposed: {0}", Bar);
   }
 }

Now do:

  IFoo f = new Foo();

  using (f)
  {
    f.Bar = 42;
  }

  Console.WriteLine(f.Bar); 

This prints:

Disposed: 42
42
4
  • In this example, using is already receiving a reference, because you are boxing in the IFoo f = new Foo().
    – xanatos
    Oct 27, 2011 at 10:55
  • @xanatos: Exactly. It is showing how to do it, if you really wanted to ;P
    – leppie
    Oct 27, 2011 at 11:00
  • 2
    Then write it! The fact that casting to an interface a value type causes boxing (outside of the generic-with-constraint case) isn't evident for 75% of the C# programmers :-)
    – xanatos
    Oct 27, 2011 at 11:05
  • @xanatos: I really dont want to encourage this :) It is acceptable though if the coder knows what he/she is doing exactly.
    – leppie
    Oct 27, 2011 at 11:09
2

Finally I have comprehended it :-) I'll post my view :-) :-)

Now...

using (MyType something = new MyType())

is meta-equivalent to

using (readonly MyType something = new MyType())

with readonly having the same meaning of the readonly keyword in class/struct declaration.

If MyType is a reference, then it is the reference (and not the referenced object) that is "protected". So you can't do:

using (readonly MyType something = new MyType())
    something = null;

but you can

    something.somethingelse = 0;

in the using block.

If MyType is a value type, the readonly "modifier" extends to its fields/properties. So they didn't introduced a new type of "const-ness/readonly-ness" in using, they simply used the one they had.

So the question should be: why can't I modify fields/properties of readonly value types?

Note that if you do this:

public void Dispose()
{
    Console.WriteLine("During dispose: {0}", n);
}

var sss = new MyStruct();

using (sss)
{
    sss.n = 12;
    Console.WriteLine("In using: {0}", sss.n); // 12
}

Console.WriteLine("Outside using: {0}", sss.n); // 12

Result

In using: 12
During dispose: 0
Outside using: 12

so the using is doing a "private" copy of sss, and sss.n = 12 is accessing the "original" sss, while Dispose is accessing the copy.

8
  • @leppie I have to tell the truth, the fact that fields and properties of read-only value types are read-only isn't something "evident" There isn't any real evident reason for this (considering that the value type is still mutable by methods, and that properties are more similar to methods than to fields). And the fact that the two "read-onlyness" are the same isn't evident either.
    – xanatos
    Oct 27, 2011 at 11:07
  • readonly has nothing to do with this. It is the semantics of structs vs classes. the former is by value, the latter is by reference. If you 'reference' the former, a copy of the value is made, the latter a copy of the reference is made (but still pointing to the original object).
    – leppie
    Oct 27, 2011 at 11:13
  • Also the fact that the variable name is the same (but different location, iow a fresh variable shadowing the previous) is confusing.
    – leppie
    Oct 27, 2011 at 11:14
  • @leppie This is pure smoke. If something is "readonly" then writing to it should make everything explode. The JIT should at least substitute calls to methods of readonly instances of structs with methods that throw an Exception (and the JIT knows exactly when a struct is readonly in safe code, because it can't be ref/out, if it's unboxed then it's a copy, if it's a parameter of a function then it's a copy). As is, it's only a "it's possible to do it, but we won't let you". The evidence of this is that properties are forbidden. It's a synctactical rule and not a "by necessity" rule.
    – xanatos
    Oct 27, 2011 at 11:18
  • 1
    Hi, I came from the future to say it's not really meta-equivalent to readonly fields. Readonly struct fields create defensive copies when a method is called, variables created in using statements don't, so they can still be mutated by calls to their methods. Also, it'd be the mutated struct the one that is disposed. See for yourselves: dotnetfiddle.net/EPjjCN Aug 7, 2021 at 7:14
1

Within the using block, the object is read-only and cannot be modified or reassigned.

check this link: http://msdn.microsoft.com/en-us/library/yh598w02.aspx

2
  • This answer is incorrect re: C# as it stands - the struct cannot be reassigned, but it can be mutated (its contents can be modified). But mutable structs are a terrible idea anyway, so arguably this answer is more correct than the language itself. :) Oct 27, 2011 at 10:36
  • I agree with @DanielEarwicker here.
    – leppie
    Oct 27, 2011 at 10:40
1

I am not 100% sure on this, so anyone please correct me if I'm wrong.

The reason that the compiler allows you to modify fields of a class in this scenario, but not a struct, is related to memory management. In the case of a class (i.e a reference type), the not the object, but the reference itself is the value. So when you modify a field in that object, you are not manipulating the value, but a memory block somewhere else, referenced by the value. In the the case of a struct, the object is the value, so when you manipulate a field in the struct, you are in fact manipulating the value that is considered read-only at this time.

The reason the compiler will allow the method call (that in turn modifies a field) is simply that it cannot analyze deeply enough to determine whether the method performs such modification or not.

There is a (closed) case at MS Connect regarding this, that may shed some more light: Cannot assign to field of struct with IDisposable and when it is a using variable - CS1654 error

11
  • Not quite correct. Look at the code generated for using (and foreach), both create a temporary variable that is assigned to. In the case of the struct, the value is copied.
    – leppie
    Oct 27, 2011 at 10:34
  • @leppie: yes, but note that the OP is not assigning to the using variable, but a field in the object (it's a mutable struct). Or did I misunderstand you? Oct 27, 2011 at 10:37
  • The OP is assigning to a field on a copy of the struct. WHile technically the code is valid, the compiler rejects it, as it will have no effect (and indicates a logical error).
    – leppie
    Oct 27, 2011 at 10:39
  • This explains the rule, but not the reason behind the rule. What was the problem they tried to resolve by introducing this rule?
    – xanatos
    Oct 27, 2011 at 10:40
  • To add to above comment, with some tricks you can get around this constraints. Hint: boxed value types.
    – leppie
    Oct 27, 2011 at 10:41
0

The struct is flagged as read-only. You're trying to modify a public member variable, which the compiler flags as read-only and so prohibits.

However, the call to Set() is allowed because the compiler has no way of knowing the the call will mutate the state of the struct. It is, in effect, a crafty way of mutating a readonly value!

Take a look at Eric Lippert's post of mutating readonly structs for more info.

6
  • This has nothing to do with readonly. Fields are never implicitly readonly.
    – leppie
    Oct 27, 2011 at 11:07
  • Also, using Set() to mutate a readonly field will not work. Compiler error.
    – leppie
    Oct 27, 2011 at 11:07
  • @leppie: Within Set() the field isn't readonly. However, in the example the struct instance is readonly as it's within a using block. This compiler infers from this that any public field on the struct will also be readonly.
    – Sean
    Oct 27, 2011 at 11:15
  • You are mistaken. The compiler does not infer any such details. You cannot mutate a readonly field (note, not talking about value semantics as per structs now, which I suspect you are referring to) except in the constructor.
    – leppie
    Oct 27, 2011 at 11:18
  • @leppie: A storage location of a structure type is nothing more nor less than then fields of that type concatenated together. It is not possible to write to the fields of a struct residing in a read-only storage location, nor is it possible to pass a read-only struct location as a ref parameter. Since instance members on structure types take this as a ref parameter, it's not really possible to invoke instance members on read-only structures. Unfortunately, C# assumes that if readOnlyStruct.method() can't pass readOnlyStruct to method as a ref parameter, ...
    – supercat
    Oct 9, 2013 at 21:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.