Why Dependency property are declared as static readonly?

后端 未结 3 2179
傲寒
傲寒 2021-02-20 09:41

It is clear to me why dependency property are static and the question still remain on my mind is why we need to use Readonly keyword at the time of declaration of Dependency Pro

3条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-02-20 09:50

    Conceptually a dependency property is something that a dependency object simply has and that does not depend on when you use the property. Just like a CLR property, if you ask does this object have a Total property, you know it cannot be a double now but an int later. As a result, we'd make the dependency property const if we could, but we cannot, so readonly is the next best thing.

    Using the readonly keyword has at least three effects:

    • it informs readers of the code that the value will not change
    • it prevents the author from accidentally changing the value
    • it assists the compiler, which benefits from knowing when things will not change

提交回复
热议问题