0

I have a FullClass with some values. I want to copy some of it's values and hold it as a copy so I created coresponding CopyClass. How can I rewrite this code to achieve object creation without new keyword and just by assigning value like in the example - for simpler use.

class FullClass
{
    public int x;
    public int y;
    public int z;
}

class CopyClass
{
    public int x;
    public int y;

    public CopyClass(FullClass fullObject)
    {
        x = fullObject.x;
        y = fullObject.y;
    }
}

void AssignCopyObject()
{
    //I want small copy of FullClass with some of it's values
    CopyClass copyObject;

    //How I create small class object
    copyObject = new CopyClass(fullObject);

    //How I want to create it automaticly by using assign of full class object
    copyObject = fullObject; //ofc doesn't work
}
Bizio
  • 45
  • 7
  • 2
    Sounds like your looking for [Automapper](https://automapper.org/) – Liam Dec 09 '19 at 14:53
  • 1
    I don't understand the question. What is wrong with the copy constructor? –  Dec 09 '19 at 14:53
  • @amy Nothing, it works :D I was just wondering if you can write it like I mentioned. – Bizio Dec 09 '19 at 14:55
  • There are different answers to this question depending on your real use case and what you want to do; using the copy constructor is clear and shows that it is not a reference to the same object (which you could hide using implicit conversions); using inheritance confuses mutablility and you should inherit behaviour not structure. – Pete Kirkham Dec 09 '19 at 14:55
  • @Bizio you can, but then the next person to read the code may not realise it's a copy not a reference. – Pete Kirkham Dec 09 '19 at 14:56
  • it looks like you are asking [can I override the assignment operator](https://stackoverflow.com/questions/18782218/is-it-possible-to-override-assignment-in-c-sharp). No you cannot, you will always need a call of `new` (either here or in a different method) because otherwise you will not have a deep copy. – Mong Zhu Dec 09 '19 at 14:57
  • or do you actually intend to have a shallow copy? – Mong Zhu Dec 09 '19 at 14:59
  • If fields from your 2 classes have the same name and you are not concerned with app performance just use reflection, no need in manual mapping. Take a look here https://stackoverflow.com/questions/7649324/c-sharp-reflection-get-field-values-from-a-simple-class – Nick Dec 09 '19 at 15:09

1 Answers1

5

You can do this using an implicit conversion operator:

class CopyClass
{
    public int x;
    public int y;

    public CopyClass(FullClass fullObject)
    {
        x = fullObject.x;
        y = fullObject.y;
    }

    public static implicit operator CopyClass(FullClass fc) => new CopyClass(fc);
}

But this somewhat obfuscates your intent.

Personally I'd go with new CopyClass(fullObject);, as that makes clear you are creating a copy of a different type.

Johnathan Barclay
  • 18,599
  • 1
  • 22
  • 35
  • OP is asking "achieve object creation without new keyword " you answer actually just shifts the new keyword into another place. – Mong Zhu Dec 09 '19 at 15:11
  • 2
    @MongZhu I think you are interpreting the question too literally. The OP wants to write `copyObject = fullObject;`, and this will allow them to do that. – Johnathan Barclay Dec 09 '19 at 15:13
  • You are probably right :) apparently Bizio is content with your solution. I tend to be pedant here on SO. Have an upvote :) – Mong Zhu Dec 10 '19 at 08:59