loading...

re: Caller Member Info, C#, and You VIEW POST

TOP OF THREAD FULL DISCUSSION
re: Loved the article!! Didn't know about this feature until now. When should I use it instead of overload or just passing an nullable parameter?
 

My argument is that you should always use CallerMemberName in cases where you want to pass a member name as it prevents mistakes.

Really, the only scenario I could use it and don't is like this:

public void LogSomething(string message, [CallerMemberName] caller = "")
  => LogSomething(message, LogPriority.Normal, caller);

public void LogSomething(string message, LogPriority priority, [CallerMemberName] caller = "") {
  // Do some logging
}

In this example, if I didn't pass in caller to the other method via the overload, caller would default to LogSomething.

So, really, that's the only case where I would explicitly set caller - when I need to pass data around and don't want CallerMemberName to overwrite the value.

code of conduct - report abuse