Where is body of PropertyChanged?

I'm wondering where is the body(instance) of PropertyChanged ?

    private int age;
    public int Age
    {
        get { return age; }
        set 
        { 
            age = value;
            OnPropertyChanged("age");
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    void OnPropertyChanged(string name)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(name));
    }

In the above, "public event PropertyChangedEventHandler PropertyChanged" is just a declaration but after the next line suddenly PropertyChanged event handler looks instantiated and then be able to be called. What happens ?

To add some information, I work using WPF. There is no explicit event subscriber code. But in the xaml there is a clue.

<TextBox x:Name="txtAge" Text="{Binding Age}/>

So, does something add subscriber code automatically when there is Binding in xaml ?

Jon Skeet
people
quotationmark

This is a field-like event. It's a bit like an automatically-implemented property - the C# compiler provides the implementation. (I'd prefer it if the syntax were more like automatically implemented properties, but that's a different matter.)

From section 10.8.1 of the C# 5 spec:

When compiling a field-like event, the compiler automatically creates storage to hold the delegate, and creates accessors for the event that add or remove event handlers to the delegate field. The addition and removal operations are thread safe, and may (but are not required to) be done while holding the lock (§8.12) on the containing object for an instance event, or the type object (§7.6.10.6) for a static event.

Thus, an instance event declaration of the form:

class X
{
     public event D Ev;
}

will be compiled to something equivalent to:

class X
{
    private D __Ev;  // field to hold the delegate
    public event D Ev {
        add {
            /* add the delegate in a thread safe way */
        }
        remove {
            /* remove the delegate in a thread safe way */
        }
    }
}

Within the class X, references to Ev on the left-hand side of the += and –= operators cause the add and remove accessors to be invoked. All other references to Ev are compiled to reference the hidden field __Ev instead (§7.6.4). The name “__Ev” is arbitrary; the hidden field could have any name or no name at all.

people

See more on this question at Stackoverflow