Using string constant for notify property changed

前端 未结 4 1358
-上瘾入骨i
-上瘾入骨i 2021-01-15 13:49

I am working with some existing code and trying to figure out the advantage (if any) of using a string constant for the name of a property when implementing INotifyPropertyC

4条回答
  •  臣服心动
    2021-01-15 13:57

    There isn't an advantage compiler wise, since both will end up being a constant value.

    I can't imagine a real advantage in using the code like that way. Either ways it is easy to make a typo, and you are not going to reuse that constant for anything, so it is pointless.

    I had love to see the new nameof keyword implement in the next version of .NET. Or even better, if possible, use [CallerMemberName] as Marc Gravell suggested.

    The use of nameof will be useful when having custom calculated properties (like in WPF for example) that don't have their own getter / setter.

提交回复
热议问题