tags:

views:

164

answers:

5

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;
A: 

C# compiler generates backend field for you. This syntax was introduced for anonymous types support (like new { A = 1, B = "foo" } )

STO
What's auto-properties got to do with anonymous types ?!?
Sam
Auto properties don't have anything to do with anonymous types.
Rodrick Chapman
@Sam excellent question n
David Lively
This is quite something different than autoprops... Nonrelated at all really, besides both being a improvement.
Dykam
+11  A: 

This is a new feature in C# 3.0.

http://msdn.microsoft.com/en-us/library/bb384054.aspx

In C# 3.0 and later, auto-implemented properties make property-declaration more concise when no additional logic is required in the property accessors.

Sam
+9  A: 

They're using C# auto properties. It's a convenience whereby the compiler generates the backing field for you. private set means that the property is read-only from outside the class. So, if sp.View = someView; is being used outside of the class then it will result in a compiler error.

Rodrick Chapman
Thanks for the response. I appreciate you answering that the compiler generates a backing field. It's helpful to know the "what happens". I don't know that I necessarily like them as they're not explicit in what they do and they're confusing upon first site. I see their purpose though.
Brian
A: 

Decompiling your ShellPresenter with the Red Gate .NET Reflector

public class ShellPresenter
{
// Fields
[CompilerGenerated]
private IShellView <View>k__BackingField;

// Methods
public ShellPresenter(IShellView view)
{
    this.View = view;
}

// Properties
public IShellView View
{
    [CompilerGenerated]
    get
    {
        return this.<View>k__BackingField;
    }
    [CompilerGenerated]
    private set
    {
        this.<View>k__BackingField = value;
    }
  }
}
statenjason
Decompiling isn't always an answer.
Henk Holterman
@Henk, In the case of "why/how" questions, decompiling is an answer. The decompiled version can help make sense of what's happening with the auto-properties. It shows that there really are fields being modified and returned when "empty getters/setters" are used.
statenjason
A: 

What you posted is not allowed unless the object is being set within the class itself. Here's a code sample of what is and is not allowed.

   public interface IShellView
    {

    }
    public class View:IShellView
    {
    }

    //public class SomeOtherClass
    //{
    //    static void Main()
    //    {
    //        IShellView someView = new View();
    //        //calling code 
    //        ShellPresenter sp = new ShellPresenter();

    //        //Why is this allowed? 
    //        sp.View = someView;//setting a private set outside the ShellPresenter class is NOT allowed.
    //    }
    //}

    public class ShellPresenter
    {
        public ShellPresenter()
        {
        }
        public ShellPresenter(IShellView view)
        {
            View = view;

        }
        static void Main()
        {
            IShellView someView = new View();
            //calling code 
            ShellPresenter sp = new ShellPresenter();

            //Why is this allowed? 
            sp.View = someView;//because now its within the class
        }
        public IShellView View { get; private set; }
    } 
P.Brian.Mackey