Consider the code below:
class Data
{
public string Name;
public string NameWithSufix;
}
class Behaviour
{
private Data data;
public string Name { get { return data.Name; } private set { } }
public Behaviour()
{
data = new Data()
{
Name = "My Name",
NameWithSufix = Name + " Sufix",
};
}
}
class Program
{
static void Main(string[] args)
{
Behaviour behaviour = new Behaviour();
}
}
If you run this program, it will fail with a NullReferenceException in the Name property. This and this , and Visual Studio will try to convince me of the initialization of the object and the constructors of the objects, followed by the assignment of properties - this but it does not look like. If I change the constructor body with commented code, it works. It seems that initiliazer does not actually start the constructor before trying to assign properties. Why?
source
share