Question

I just stumbled over the following:

class AFrameworkElement : FrameworkElement
{
    private void SomeMethod() 
    {
        Size s = new Size {
            Width = Height = 10
        };
        // the size isn't used for measuring oder arranging
    }
}

Suddenly I was unable to resize the Control... IntelliSense showed the problem: I put he mouse over Height and it showed that the FrameworkElement.Height property was meant. So why is it impossible to use double assignements in object initializers?

Était-ce utile?

La solution

In an object initializer expression the object itself is not accessible within the initialization expression. Anything used in that expression is interpreted as if it were used outside the object initializer. Hence in this case Height = 10 is evaluated within the context of AFrameworkElement and Height binds to a property on that object

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top