35

I'm using the nameof function to get a property name as a string thus:

public bool IsRunning => ...;

...
RaisePropertyChanged(nameof(IsRunning));

ReSharper highlights this with the warning:

Explicit argument passed to parameter with caller info attribute

The code works, I was just wondering if the above warning is something I should worry about.

Pang
  • 9,564
  • 146
  • 81
  • 122
Tim Rutter
  • 4,549
  • 3
  • 23
  • 47

2 Answers2

28

was just wondering if the above warning is something I should worry about.

When you have CallerMemberName attribute attached, you don't have to explicitly pass a value, because the attribute will do exactly that for you. It will find the caller's name and use it, making your nameof declaration redundant. This is of course assuming you call RaisePropertyChanged from the actual property implementation.

ReSharper marks these calls as redundant when you explicitly pass a string literal. It should force the same logic with nameof as well.

Pang
  • 9,564
  • 146
  • 81
  • 122
Yuval Itzchakov
  • 146,575
  • 32
  • 257
  • 321
  • Ah I get it. Hadn't quite appreciated what CallerMemberName was doing.The RaisePropertyChanged function (part of `ObservableObject` in `GalaSoft.MVVMLight`) was written before nameof existed, I guess it would be different if written now. – Tim Rutter Aug 07 '15 at 10:25
  • 6
    It's stupid because it doesn't check if name is different than current scope, so when I call explicitly to raise for another property it doesn't discover that. – Shimmy Weitzhandler Jul 13 '17 at 16:44
  • I agree Shimmy and therefore it is better to be explicit in most cases just to avoid problems in your code – Philip Stuyck Aug 02 '17 at 07:49
  • How do I disable this warning? Can you please post an example? I don't have Re#er, but I'm working on a repo that its owner has and cares about this complaint. – Shimmy Weitzhandler Aug 03 '17 at 17:15
  • 2
    In a file, you can add at the top: `// ReSharper disable ExplicitCallerInfoArgument` And just once on the line above the call: `// ReSharper disable once ExplicitCallerInfoArgument` –  Oct 18 '17 at 12:06
4

Not as long as your code is called from the IsRunning property (which makes the warning valid. Specifying the property name would be redundant in that case). It doesn't seem you are doing that.

The warning just tells you that the RaisePropertyChanged has the CallerMemberNameAttribute set on the property, which it should. It is safe to ignore.

Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
  • Ah I see its nothing to do with nameof, its the RaisePropertyChanged that is being highlighted – Tim Rutter Aug 07 '15 at 10:19
  • No, I think you understand the use I guess. (If not sure, read [here](http://stackoverflow.com/q/31695900/993547)) You just raise the event it has changed. If it did, you are okay. – Patrick Hofman Aug 07 '15 at 10:20