0

We are following Hungarian notation to declare private variables as below.

private IRepository<Request> _requestRepo;

public RequestService(IRepository<Request> requestRepo)
{
    _requestRepo = requestRepo;
}

The above code shows a sonar issue like Field name does not start with underscore. Please tell me how to declare the private variables to satisfy coding standards?

Paul Zahra
  • 9,522
  • 8
  • 54
  • 76
user3106578
  • 177
  • 1
  • 2
  • 12
  • By not making it start with an underscore, or by disabling the warning temporarily ([`SuppressMessage`](http://stackoverflow.com/questions/7092778/vs2010-code-analysis-suppress-message-ca1051donotdeclarevisibleinstancefields)) or by disabling the rule altogether. See [To underscore or to not to underscore, that is the question](http://stackoverflow.com/questions/450238/to-underscore-or-to-not-to-underscore-that-is-the-question). – CodeCaster Jan 20 '14 at 09:29

1 Answers1

0

Please don't use hungarian notation for C#, it's very, very obsolete. Use the Microsoft Coding Guidelines. The whole .NET world is designed with those in mind, going against it will make your code stick out like a sore thumb.

That said, the guidelines aren't clear about rules for private fields. It's common to either use an underscore or camelCase. If you use Microsoft Visual Studio, use the Code Analysis feature (formerly known as FxCop) to get hints like make your private variable readonly in this case.

Your error message does not make any sense. If the message says your field name should not start with an underscore, I'd suggest camelCase as per the Microsoft Guidelines.

nvoigt
  • 75,013
  • 26
  • 93
  • 142