9

I just relfected over WindowsBase.dll >> System.Windows.UncommonField<T> and I wondered about the usage of this class... E.g. it's used in the Button-class:

public class Button : ButtonBase
{    
    private static readonly UncommonField<KeyboardFocusChangedEventHandler> FocusChangedEventHandlerField = new UncommonField<KeyboardFocusChangedEventHandler>();
}

So what is the use of this "wrapper"?

2
  • 3
    This is internal class, so you can't use it. Aug 9, 2013 at 18:15
  • 3
    I don't know what that is, but I would not worry too much about it. Since it's internal, and is not intended to be used by your code. Aug 9, 2013 at 18:15

1 Answer 1

11

Short answer

It's used to lower memory usage.

Longer answer

Let's start with dependency properties. On each DependencyObject, a large number of dependency properties can be defined. Whether it's a "local" DependencyProperty such as TextBox.Text or an attached one like Grid.Row, most of them aren't ever set and only keep their default value. In order to avoid having each DependencyObject instance taking kilobytes of memory by storing a value for each defined dependency property, only values being different from the defaults are kept inside the instance.

Now meet the internal UncommonField<T> class. You can view it as a lightweight DependencyProperty without any metadata, coercion or property change notification. However, it uses the same mechanism as a real DependencyProperty regarding its value: it has to be different from the default to be stored inside the DependencyObject. Since the KeyboardFocusChanged event is rarely used (explaining the uncommon adjective), it makes sense to save a little memory here.

But you're probably not Microsoft writing a framework having to account for thousands of DependencyObject instances and optimize for a few bytes. Simply replace a static UncommonField<T> someField by an instance T someField.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.