A developer’s view

Refactoring and strings

· Read in about 2 min · (247 Words)
C# nameof stringof

Refactoring is a cool feature that I start using more and more. But why on Earth, doesn’t Microsoft do something about refactoring and strings? Everytime I refactor an attribute or property I am worried about my strings. Of course, I can set the option that it should also look at strings and comments, but I am worried that it might change too much (or too little).

The solution for this would be very simple. We already have a typeof() operator and why not introduce a stringof() operator. This operator would result in a string that describes the identifier, so something like RaisePropertyChanged(stringof(this.Name)) would result in RaisePropertyChanged("Name"). This change wouldn’t impact IL, but only affect the compiler, so it would be a rather small update that would make life a lot easier.

The compiler knows the full semantics of the identifier, so if the identifier is not valid an error can be generated. The refactoring module also knows about the semantics, so it can properly update the name as well. If I would choose to refactor manually and I forget something, then an error would be generated, instead of raising the wrong event.

Currently we have this issue for raising PropertyChanged events, but also when registering dependency properties, XAML binding, …

I already submitted this feature request back in 2005, but no luck so far.

Update (October 2014)

Finally, this feature has been implemented in C# 6. Only the name is different. It has been called nameof instead.

Comments