This morning I encountered an interesting situation in an application I'm building. Even though I've resolved this in another way, I'm interested in finding out what the answer would be.
Lets say we have the following situation:
public class SomeDependency
{
public string SomeThing { get; set; }
}
public class SomeClass
{
public SomeDependency Dependency { get; }
public SomeClass() : this(new SomeDependency) { }
public SomeClass(SomeDependency dependency)
{
Dependency = dependency;
}
}
Right now there are 2 ways of initializing this class:
We could just say:
var x = new SomeClass();
That of course would give us the expected situation. But what if we do this:
var x = new SomeClass
{
Dependency = new SomeDependency { SomeThing = "some random string" }
};
We use the parameterless constructor in the latter situation but we do give Dependency
a value via object initializer, hence my question:
Will Dependency
be overwritten by the the this(new SomeDependency())
call?