I’ve been reviewing the PRISM toolkit and I find many examples where they declare a public property with empty getters/setters yet they can still set the property of the instantiated class. How/why is this possible?
public class ShellPresenter
{
public ShellPresenter(IShellView view)
{
View = view;
}
public IShellView View { get; private set; }
}
//calling code
ShellPresenter sp = new ShellPresenter();
//Why is this allowed?
sp.View = someView;
They’re using C# auto properties. It’s a convenience whereby the compiler generates the backing field for you.
private setmeans that the property is read-only from outside the class. So, ifsp.View = someView;is being used outside of the class then it will result in a compiler error.