I have code like this:
public class test {
public string aa { get; set; }
public string bb { get; set; }
public string cc { get; set; }
}
var a = new test {
aa = "a",
bb = "b"
}
var d = a;
d.cc = "c";
Is there any way for me to create class d and at the same time populate it. So for example the last two lines could be combined into one? Note that I want to avoid doing this with a constructor as sometime it will be different fields other than cc which I will populate.
please note my edit
var d = a;
Given your new description of the problem, how about this:
Note that it changes
aas well (same as your code), it doesn’t create a copy. If you want a copy, go with Oded’s solution.I’d say this isn’t as readable as your original code though (as you need to know what
AlterTestdoes, which isn’t a lot).