Naming Conventions in C# - underscores

前端 未结 3 1949
时光说笑
时光说笑 2020-12-28 14:33

I saw this at an MVC3 Razor tutorial at http://www.asp.net

public ActionResult Index() {

    return View(_usrs._usrList);

}

Isn\'t that u

相关标签:
3条回答
  • 2020-12-28 14:44

    In ASP. NET MVC 3 underscores are used more usually. For example all your partial views you need to name with underscore like _MyPartialView.
    It's going for easy distinguishing partial views and views in your application.

    Anyway, in this example I don't prefer sing underscores, because there is no need to use them. It isn't wrong, because it's good practice to write with underline lists of your entities. But I will prefer to write without them.
    So both ways are right, write in the way you feel more comfortable.

    0 讨论(0)
  • 2020-12-28 14:46

    The guidelines are summarized here http://blogs.msdn.com/b/brada/archive/2005/01/26/361363.aspx and include the stipulation to use "this." instead of underscore. But I find that peppering my code with "this."'s makes the code more wordy, cluttered and hard-to-read. Furthermore it seems to be less often followed than underscore so as a convention, "_" seems more conventional.

    0 讨论(0)
  • 2020-12-28 15:02

    A good article to read on the development of C# style guidelines is here at StyleCop.

    The original guidance for .NET was to never use underscores unless they were part of a private member variable, and then only as a prefix, e.g. _customerId. This was probably inherited from MFC where 'm_' was used as a prefix for member variables.

    Current practice is not to use underscores at all. Disambiguation between private member variables and parameters with the same name should done using 'this.'. In fact all references to private members should be prefixed with 'this.'.

    The only place underscore seems to be used a lot is in unit test methods. I'm not a fan, but it may make the methods more readable, for example Throw_If_Customer_Is_Null(){...}.

    0 讨论(0)
提交回复
热议问题