CLR Properties vs. Dependency Properties
February 20th, 2009 | 6 Comments
Like many things in .NET, you have several ways of doing something with each way custom-tailored to a particular task. Using properties is one such example. You can either use CLR properties, or you can use dependency properties.
In case you are not familiar with CLR and Dependency Properties, let’s quickly look at them. The following is an example of a CLR property that you have had in .NET for a very long time:
- public string AuthorName
- {
- get;
- set;
- }
Introduced as a part of WPF a few years ago and carried over to Silverlight is the new mythical bird-creature known as Dependency Properties:
- public string AuthorName
- {
- get { return (string)GetValue(AuthorNameProperty); }
- set { SetValue(AuthorNameProperty, value); }
- }
- public static readonly DependencyProperty AuthorNameProperty = DependencyProperty.Register("AuthorName", typeof(string), typeof(Page), new PropertyMetadata(""));
If you compare the Dependency Property with the CLR Property, it is quite obvious that one seems to make more sense than the other. At the very least, the CLR property seems like something you can easily remember how to write. Remembering exactly what goes into the syntax for declaring a dependency property is quite a challenge…even for seasoned WPF and Silverlight developers.
Despite the difficulty, dependency properties certainly offer more functionality than a regular CLR property. If you plan on having your property’s value be set via an animation, accessed via data binding, stored as a resource, etc., you really should take the dependency property route. You can read more about this on an article I wrote a while ago describing dependency properties inside user controls.
If you are interested in a quick pictorial overview of the difference between CLR and dependency properties, look at each property type in Expression Blend. The Properties Marker in Expression Blend for a CLR property looks as follows:
Notice that most of the commands are grayed out. Contrast this with the equivalent dependency property instead:
As you can see, you retain everything you had with CLR properties, but you also gain the ability to store the value as a resource or access the value via data binding. Not to mention, you also can animate the property very easily.
Cheers!
Kirupa





February 20th, 2009 at 6:38 am
I’m a WPF guy, not a Silverlight guy, but I doubt the two differ in this respect. DPs are not required for rich data binding support. One way binding works great with a simple CLR property and two way binding works great with a CLR property that raises INotifyPropertyChanged.PropertyChanged. Under most circumstances I’d stay with a CLR property and INPC for your data models.
February 20th, 2009 at 10:19 am
That is a fair point wekempf. If you know how to raise property changes on your own, a regular CLR property will go a long way.
February 20th, 2009 at 12:35 pm
Hi wekempf,
You can use a CLR as the source of a binding, but not as the target of the binding.
Although you are able to create a 2-way databinding from a CLR property, this will be still used as the source of the binding, which differs from being the target, because 2-way binding is not completeley symmetric.
February 22nd, 2009 at 7:57 am
julian, oh yes, that’s certainly true. Didn’t realize that’s what you were referring to in this blog post.
February 28th, 2009 at 3:40 am
[...] Für alle die damit nicht vertraut sind hat Kirupa Chinnathambi einen Blogeintrag “CLR Properties vs. Dependency Properties“ [...]
September 21st, 2009 at 11:39 pm
All these points are okay. But I wanted to understand why DP were invented? WPF team could have given support for these scenarios even in CLR property system. Why DP are pushed? Is it performance? I doubt because DP seems to be more costly. Its not that DP was the only solution for databinding, animation etc…. we have animation, databainding etc. concepts on other platforms as well and even without DP.