-1

After googling I've learned that it's not possible unless you do it manually. Take this code for example:

public struct SEverything
{
    public int[] arrayOfHouseNumbers;
    public string nameOfOwner;
}

I only know of a very cumbersome way to copy it and that's to create a new struct and do it like this:

private void CopySEverything()
{
    sEverythingCopy = new SEverything();
    sEverythingCopy.nameOfOwner = sEverythingOriginal.nameOfOwner;
    sEverythingCopy.arrayOfHouseNumbers = new         
    int[sEverythingOriginal.arrayOfHouseNumbers.Length];

    for (int i = 0; i < sEverythingCopy.Length; i++)
    {
        sEverythingCopy[i] = sEverythingOriginal[i];
    }
}

I'm using a much more complicated struct and it would be really annoying to do the above for a big struct, not to mention that if the structure of the struct is changed, the code has to be rewritten. Any advice is very welcome.

e: is serializing and deserializing a good option?

  • structs are value types, so deep copy doesn't quite apply unless I'm missing something? – The Bearded Llama Jul 05 '17 at 10:41
  • 1
    @TheBeardedLlama: In what way would it not apply? The `arrayOfHouseNumbers` field is just a reference... – Jon Skeet Jul 05 '17 at 10:42
  • 4
    I would question your use of a struct for this to start with - mutable structs and public fields are both usually a bad idea. – Jon Skeet Jul 05 '17 at 10:43
  • Take a look at [AutoMapper](http://automapper.org/) – Fabiano Jul 05 '17 at 10:44
  • 1
    Side note: `Array.Clone` exists, so simple scalar arrays like `arrayOfHouseNumbers` do not need to be copied element for element. That you have a `struct` rather than a `class` is also not particularly relevant; deep cloning is essentially the same problem for both (being value types, structs just give you memberwise cloning for free -- but nothing prevents you from exposing `Object.MemberwiseClone` in your own class). Note that an array in a struct is still just a reference, not data actually embedded in the struct. It's questionable if you really want a struct here. – Jeroen Mostert Jul 05 '17 at 10:46
  • @JonSkeet I was thinking generally of the theory behind value types; as you said in this case there's a reference, so that changes things (useless comment, apologies); but yes I agree mutable structs are generally not a good idea – The Bearded Llama Jul 05 '17 at 11:00

1 Answers1

0
private void CopySEverything()
{
    sEverythingCopy = new SEverything();
    sEverythingCopy.nameOfOwner = sEverythingOriginal.nameOfOwner;
    sEverythingCopy.arrayOfHouseNumbers = new         
    int[sEverythingOriginal.arrayOfHouseNumbers.Length];

    for (int i = 0; i < sEverythingCopy.Length; i++)
    {
        sEverythingCopy[i] = sEverythingOriginal[i];
    }
}

could be simplified to:

private void CopySEverything()
{
    sEverythingCopy = new SEverything();
    sEverythingCopy.nameOfOwner = sEverythingOriginal.nameOfOwner;
    sEverythingCopy.arrayOfHouseNumbers = sEverythingOriginal.arrayOfHouseNumbers.ToArray();
}

You should also consider https://github.com/Burtsev-Alexey/net-object-deep-copy/blob/master/ObjectExtensions.cs .

mjwills
  • 23,389
  • 6
  • 40
  • 63