Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How is the WPF property system economical?

It is said that the designers of WPF have made it economical or higher performance. Can someone please explain with an example of what happens under the hood that makes the WPF property system more economical?

like image 984
paseena Avatar asked Apr 05 '11 13:04

paseena


1 Answers

You are probably referring to the fact that Dependency Properties are "cheaper" than normal CLR properties.

In a few words:

Dependency properties are implemented using sparse data structures that only allocate memory for the property value if it is set on an object. In contrast, a standard CLR property value is stored as a field inside every object of the class in which the property is defined, even if all of these objects have the property set to its default value.

So for example, if we have 100 objects with 100 CLR properties of type int each, then we are using 10000 ints worth of memory even if all of those have the same default value (0).

If the property were a dependency property instead, we would not be using any additional memory at all: WPF does not need to remember the value of any property since it knows that you didn't change it from the default.

Of course the above is rather a simplistic explanation and does not cover all the advantages of dependency properties over CLR properties, but it should explain the "DPs have higher performance" statement adequately.

like image 96
Jon Avatar answered Sep 21 '22 10:09

Jon