Search code examples
wpfwpf-controls

WPF Setting property to its default value


In WPF, Does setting a default value to the property have performance impact?

For example : SolidColorBrush x:Key="ButtonRed" Color="#FFEF0137" Opacity="1"

As default value for Opacity is 1, does setting the value again a bad practise?


Solution

  • Does setting a default value to the property have performance impact?

    No. At least not in practice, i.e. it's not something that you will notice or even be able to measure.

    Theoretically speaking, it's another attribute (some more text/bytes) for the XAML processor to parse and handle.

    As default value for Opacity is 1, does setting the value again a bad practise?

    I don't know if I would call it bad practice but it's certainly unnecessary.

    As mentioned in a comment, local values take precedence over values set by styles even of this doesn't apply to brushes.

    When it comes to performance, the general rule of thumb is that you should first identify a performance issue before you start to optimze the actual bottleneck. Setting properties of XAML resources isn't likely to be a performance killer.