Hi I was using C# Object Initializer like
public class Invoice
{
public decimal GrossSum { get; set; }
public decimal GrossSumComp { get; set; }
}
public class ABC()
{
public Invoice Invoice {get;set;}
public ABC(decimal grossSum)
{
Invoice=new Invoice()
{
GrossSum=grossSum,
GrossSumComp=**GrossSum**
};
}
}
And I saw we can’t assign value of One Property to another in it , Like above I tried to assign GrossSum to GrossSumComp and there I got Compilation error. Just curious to know Why It dont allow this. Any help will be highly apericaited.
Because that’s how it’s specified.
GrossSumin the second line is a reference to a variable namedGrossSumnot a property of the object being initialized.To paraphrase Eric Lippert. For a feature to be implemented it has do be
Those all incur a cost, so they should add value to match the cost and preferably exceed the cost.
If there’s a simple work around to the feature, odds are that the cost is likely to be (a lot) higher than the potential value.
In your case you can simply assign
grossSuminstead ofGrossSum. That’s a simple work around