ReSharper 2024.2 Help

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 PageVisibility property, the nameof expression will force you to update the argument to reflect the new name. This way you ensure the consistency and reduce 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: 26 September 2024