I defined a class:
public class Sample{
public string name {get;set;}
public int price {get;set}
}
Then
Sample sampleA = new Sample();
sampleA.name = "test";
Sample sampleB = new Sample();
sampleB.price = 100;
I do this because I will save JSONed sampleA and JSONed sampleB to Azure table, to present a full sample object. In other code, sometime only need name, sometime only need price, so I only need to pull data that needed each time. Of course in real code, the structure of the sample is much much more complex.
My question is:, is there any easy method that can do:
Sample sampleC = sampleA + sampleB;
and sampleC should contain:
sampleC.name = "test";
sampleC.price = 100;
This has actually nothing to do with partial classes. Partial class is a single class, which is "geographically" declared more than in one file.
File1.cs:
public partial class File { public string Prop2 { get;set; } }
File2.cs:
public partial class File { public int Prop1 { get;set; } }
which will, when compiled, produce:
public partial class File
{
public string Prop2 { get;set; }
public int Prop1 { get;set; }
}
For what you are asking.
There is no such method, which would combine two different instances into one. You should write it by your own.
UPDATE:
You may ask, why there is no such method. But how would it handle situation like:
Sample sampleA = new Sample();
sampleA.name = "test";
sampleA.price = 200;
Sample sampleB = new Sample();
sampleB.price = 100;
Sample sampleC = sampleA + sampleB; // what would be the value of price here: from sampleA or sampleB?
How about you just overload the +
operator
?
public static Sample operator +(Sample left, Sample right)
{
left.price = right.price;
return left;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With