Code inspection: Use 'nameof' expression when registering a DependencyProperty
This inspection suggests using the nameof
expression when registering a DependencyProperty
because the nameof
expression makes your code more maintainable and less error-prone.
In the example below, using nameof(PageVisibility)
instead of "PageVisibility"
makes the code more robust and provides compile-time checking of the property name. If you later rename the property PageVisibility
, the nameof
expression will automatically update to reflect the new name, ensuring consistency and reducing the risk of runtime errors.
readonly DependencyProperty PageVisibilityProperty =
DependencyProperty.Register("PageVisibility",
typeof(Visibility),
typeof(PagePreview),
null);
readonly DependencyProperty PageVisibilityProperty =
DependencyProperty.Register(nameof(PageVisibility),
typeof(Visibility),
typeof(PagePreview),
null);
Last modified: 25 September 2024