为什么Dependency属性被声明为静态只读? [英] Why Dependency property are declared as static readonly?

查看:100
本文介绍了为什么Dependency属性被声明为静态只读?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对我来说很清楚,为什么依赖项属性是静态的,而我仍然想知道的问题是为什么我们在声明依赖项属性时需要使用Readonly关键字。

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 Property.

推荐答案

从概念上讲,依赖项属性是依赖项对象仅具有的东西,与依赖项的使用时间无关。就像CLR属性一样,如果您询问此对象是否具有 Total 属性,则知道它不能是 double 现在,但 int 之后。结果,如果可以,我们将使依赖属性 const 成为可能,但我们不能这样做,因此 readonly

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.

使用 readonly 关键字至少具有三个作用:

Using the readonly keyword has at least three effects:


  • 它告知读者代码值不会改变

  • 它防止作者意外更改值

  • 它有助于编译器,它受益于知道什么时候不会改变

这篇关于为什么Dependency属性被声明为静态只读?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆