Consider I have
public class ClassA
{
public string PropertyB { get; set; }
}
And then I use it like this
public class ClassD
{
static readonly ClassA PropertyE = new ClassA();
static ClassD()
{
PropertyE.PropertyB = "valueF";
}
}
but the rest of the code didn't work as I expected. Then I rewrote ClassD, and it worked
public class ClassD
{
static readonly ClassA PropertyE = new ClassA { PropertyB = "valueF" };
}
In which way are these two code samples different? I expected that they had the same behavior, but they don't.
According to MSDN:
The only difference between your two classes is how
PropertyEis initialized. In the first, sample,ClassD.PropertyEis assigned first, thenClassA.PropertyB. In the second sample,ClassA.PropertyBis assigned first, thenClassD.PropertyE. This could produce slightly different results.You might also have issues with circular dependencies among fields. As the MSDN article states: