1

I am going through a tutorial on MVC - Linq to SQL. Here, I noticed they are using underscore in object names (first character in object name) such as "_dataContext"

Here is the code:

using System.Collections.Generic;
using System.Linq;

namespace MvcApplication1.Models
{
         public class MovieRepository : IMovieRepository
         {
              private MovieDataContext _dataContext;

              public MovieRepository()
              {
                    _dataContext = new MovieDataContext();
              }

              #region IMovieRepository Members

              public IList<Movie> ListAll()
              {
                   var movies = from m in _dataContext.Movies
                        select m;
                   return movies.ToList();
              }

              #endregion
         }
     }

My questions is, what is the purpose of using this underscore?

user793468
  • 4,898
  • 23
  • 81
  • 126

6 Answers6

8

My questions is, what is the purpose of using this underscore?

No technical purpose. This is merely a convention used by some people to designate private fields. It is completely optional, and merely a style choice.

Reed Copsey
  • 554,122
  • 78
  • 1,158
  • 1,373
  • 4
    The best advantage I've found to it is that intellisense/debugging windows are sorted so all the backing fields appear in one place together. The worst advantage I've found is its ability to provoke heated debates about coding style. – Hans Jonus Feb 02 '12 at 22:28
  • @HansJonus I agree - coding style is very personal. I, personally, use lower camel case, but only because StyleCop will enforce this, and having a tool helps guarantee project consistency. However, there is no technical reason to use any convention here. – Reed Copsey Feb 02 '12 at 22:36
1

Underscores by convention signify a private member, but have no actual effect. Also, any non-private members that start with underscore will throw a compiler warning.

kitti
  • 14,663
  • 31
  • 49
1

It's so you don't have to come to stack overflow, to explain why this gives you a stack overflow

private int someValue;

public int SomeValue {get {return someValue; } set {SomeValue = value;}}

Names differing just by case, don't like it...

Tony Hopkinson
  • 20,172
  • 3
  • 31
  • 39
0

That's basically to allow at-a-glance distinguishing between class level fields and local variables and method parameters.

Erik Dietrich
  • 6,080
  • 6
  • 26
  • 37
0

To delineate private instance variables from other types of fields. It is a convention; there is no semantic value.

BTW, the "official" MS C# programming guidelines suggest camel case for private class variables. The decision is up to you and your team, but personally I don't like it because the names will often conflict with method parameters. I use the underscore myself, but again, use whatever works best for you and other maintainers of the code. The only hard rule is to be consistent.

Ed S.
  • 122,712
  • 22
  • 185
  • 265
0

I've seen this used in projects where programmers had a VB.NET background: as VB is mostly case insensitive this will help distinct between fields and properties (i.e. it's a coding convention to indicate private fields, I'd think).

VB programmers would write:

Public Class Princess
  Private _pretty
  Public ReadOnly Property Pretty As Boolean
      Get
          Return _pretty
      End Get
  End Property
End Class

Whereas C# programmers may write

public class Princess 
{
  private bool pretty;
  public bool Pretty { get { return pretty; } }
}

Note that in both cases these kind of princesses by default would be ugly.

MyKuLLSKI
  • 5,285
  • 3
  • 20
  • 39
Jeroen
  • 60,696
  • 40
  • 206
  • 339