0

I have an structure and a class

public class MyClass
{
    public string name;
}

public struct MyStructure
{
    public MyClass classValue;
    public int intValue;
    public MyStructure(MyClass classValue, int intValue)
    {
        this.classValue = classValue;
        this.intValue = intValue;
    }
}

Elsewhere I write the following code:

MyClass class1 = new MyClass() { name = "AA" };
MyStructure struct1 = new MyStructure(class1, 4);
MyStructure struct2 = struct1;
struct1.classValue.name = "BB";  //struct2.classValue.name also change!

How can I make sure that every reference type member of a structure is copied by value in cases such as this?

Chris Pfohl
  • 18,220
  • 9
  • 68
  • 111
Hossein Narimani Rad
  • 31,361
  • 18
  • 86
  • 116
  • 1
    `struct` was not a good choice for `myStructure`. That's the root of the problem. – H H Jan 22 '13 at 19:36
  • @HenkHolterman is correct. Why do you want `struct` semantics for this? There are good reasons to use `struct`s, but the situations are very specific. – Chris Pfohl Jan 22 '13 at 19:41
  • 1
    See also http://stackoverflow.com/questions/11336935/c-sharp-automatic-deep-copy-of-struct – 500 - Internal Server Error Jan 22 '13 at 19:43
  • 1
    @HenkHolterman Maybe you should explain why you don't think `MyStructure` should be a `struct`. It consists of one object reference and one `Int32`. As it is written, it is a mutable `struct`, but that's not related to the question. It could be made immutable. Same behavior will be found for `KeyValuePair` which is a `struct`. – Jeppe Stig Nielsen Jan 22 '13 at 19:45
  • 1
    @ChristopherPfohl Would changing to a `class` help him? No. He seems to want a deep copy, and a `struct` does not give him that. A class does not even give him a shallow copy (it just gives a new reference to the same object), so it's not really closer to what he wants. It sounds like he chose `struct` because he wanted copy-by-value semantics. This is the valid reason to choose `struct`. – Jeppe Stig Nielsen Jan 22 '13 at 19:57

2 Answers2

1

Your MyStructure consists of an object reference of type MyClass and an int (Int32). The assignment

MyStructure struct2 = struct1;

copies just those data, the reference and the integer. No new instance of MyClass is created. You can't change that.

If you want to copy, you can say

MyStructure struct2 = new MyStructure(
    new MyClass { name = struct1.classValue, },
    struct1.intValue
    );

Of course, if you need to do this a lot, you can write a Clone() method to do just this. But you will have to remember to use the Clone method and not just assign.

(By the way, the MyStructure is a "mutable struct". Many people discourage them. If you mark the fields of MyStructure as readonly, I think "many people" will be satisfied.)

Addition: See also Answer to Can structs contain references to reference types in C#.

Community
  • 1
  • 1
Jeppe Stig Nielsen
  • 60,409
  • 11
  • 110
  • 181
  • 1
    It's far less dangerous for a struct to have fields that allow piecewise mutation, than for a struct to encapsulate state in objects that may be mutated. It's fine for a struct to have the *identity* of a mutable class object as part of its state (as would be the case with e.g. a `KeyValuePair` but structures which encapsulate the state of a mutable object as part of their own state tend to have very confusing semantics. – supercat Feb 10 '13 at 18:39
0

The assignment operator will not deep-copy a reference-type field inside of a struct. You can create your own copy constructor that will perform the deep-copy manually, however:

public MyStructure(MyStructure structure)
{
    classValue = new MyClass() { name = structure.classValue.name };
    intValue = structure.intValue;
}

Then call the copy constructor using:

MyStructure struct2 = new MyStructure(struct1);

Alternatively, you could change MyClass to a struct.

Brett Wolfington
  • 6,587
  • 4
  • 32
  • 51
  • I also thought about changing `MyClass` to a struct. But it seems like he needs to "mutate" `MyClass` instances, so making it a `struct` would lead to a "mutable struct" which many people consider evil. – Jeppe Stig Nielsen Jan 22 '13 at 20:23
  • Structs nested within *mutable exposed-field* structs work nicely; from a performance standpoint, the fields of the inner struct may be accessed as though they were simply fields of the outer struct. Structs which are encapsulated as properties of other types or read-only fields work less well, the system will have to make a redundant copy of such structures to access any properties thereof. – supercat Feb 10 '13 at 23:15