How to create a new object containing all the values of an initial object but adding new attributes in C#

Asked

Viewed 48 times

1

Hello, I have a question regarding C# and the optimization of the form of code generation. In Typescript, if I have the code below I can create a new object from a previous one and, in the creation itself of that object I can add to the same new properties, like this:

class Foo {
    Bar: string;
    Bin: number;
}

const foo = new Foo();
foo.Bar = 'teste';

const foo2 = {...foo, Bin: 123};

in C# I am obliged to define the foo2 defining property in property, which (in my view) reduces the declaration speed in the code, thus:

public class Foo {
    public string Bar { get; set; }
    public int Bin { get; set; }
}
public class Program {
    public static void Main(string[] args)
    {
        var foo = new Foo() {
           Bar = "teste" 
        };
        var foo2 = new Foo() {
           Bar = foo.Bar,
           Bin = 123
        };
    }
}

In this example I have only 1 property, so it is not so bad to replicate the statement, but if I have a class with many properties, it is already costly and tiring...

Is there any way that I can infer from the initialization of an object that it will have the properties of another of the same type and also have new properties? What you’re looking for would be something like this:

// ... código aqui...
var foo2 = new Foo(){ ...foo, Bin = 123 };
  • I don’t understand, because there’s even a way to cross a array C# with the word reserved params, but, your example is not based on that, could edit and put a better example?

2 answers

1

c# syntax does not allow Typescript inline cloning and recognition {...foo, Bin: 123};.

What you can do is create a constructor that initializes some members of the class and emits a clone using the method Object.Memberwiseclone.

public class Foo {
    public string Bar { get; set; }
    public int Bin { get; set; }

    public Foo(out Foo foo, string bar , int bin) {
       Bar = bar;
       Bin = bin;
       foo = (Foo)this.MemberwiseClone();
    }

}

public class Program {
    public static void Main(string[] args)
    {

        var foo1 = new Foo(out Foo foo2, "PT Stack Overflow", 17) { 
            Bar = "foo1 modificado com sucesso" };
        }

        Console.WriteLine("foo1.Bar = " + foo1.Bar + "| foo2.Bar = " + foo2.Bar);
        Console.WriteLine("foo1.Bin = " + foo1.Bin + "| foo2.Bin = " + foo2.Bin);

    }
}

Which will result in:

foo1.Bar = foo1 modificado com sucesso | foo2.Bar = PT Stack Overflow
foo1.Bin = 17 | foo2.Bin = 17

0

You probably want to use a parameter in the constructor:

public class Foo
{
    public string Bar { get; set; }
    public int Bin { get; set; }

    // caso ainda não possua uma instância
    public Foo() { }

    public Foo(Foo outro)
    {
        if (outro == null) return;
        Bar = outro.Bar;
        Bin = outro.Bin;
    }
}

If you want to use something more dynamic, take a look at Automapper

Browser other questions tagged

You are not signed in. Login or sign up in order to post.