Here is the situation
public class A {
public string Prop1 {get;set;}
public int Prop2 {get;set;}
}
A a = new A(){ Prop1="A", Prop2 = 1}
A b = new A(){ Prop1="B"}
//Merge Two objects into a but only specific properties
a.Prop1 = a.Prop1 != b.Prop1 ? b.Prop1 : a.Prop1; //This part.
//a = {Prop1: "B", Prop2: 1}
Is there a better way to do this? Right now I’ve got 20 properties and I’m just copy/pasting this line with small changes over and over again. In Javascript with jQuery this would just by $.extend(a, b). I know several questions have asked that very question but I’m looking for that specific awful line.
Thanks!!
Since your properties don’t have any logic in their setters, it’s unclear why comparing the old vs. new value matters; in the end, your code’s logic will ensure that the property values in b are always what’s set.
In other words, this would accomplish the same goal: