Do you prefer having `this.` prefixes in code?
koistya opened this issue · 2 comments
What is your preferred code style from these two:
(prefix private fields with _
, then reference them without using this.
)
public class UserStore
{
private readonly ApplicationDbContext _db;
public UserStore(ApplicationDbContext db)
{
_db = db;
}
public IQueryable<User> Users
{
get { return _db.Users; }
}
public Task CreateAsync(User user)
{
_db.Users.Add(user);
return SaveChanges();
}
}
or
public class UserStore
{
private readonly ApplicationDbContext db;
public UserStore(ApplicationDbContext db)
{
this.db = db;
}
public IQueryable<User> Users
{
get { return this.db.Users; }
}
public Task CreateAsync(User user)
{
this.db.Users.Add(user);
return this.SaveChanges();
}
}
I was quite interested in using 'this' instead of '_' to prefix the private members of the classes that I write.
However, Resharper and other modern code-refactoring tools, suggest prefixing '_'
(underscore) to make sure that it is a private member of the class.
So, going forward, I think it is better to use ' _ ' (underscore) than using 'this'. Interestingly, we also type less code if we use a ' _ ' (underscore).
Hi guys, agreed about using '_'. This is pretty known standard which came from Microsoft if I'm correctly remember. And it is shorter and better noticeable if you're using editor w/o syntax highlight (but probably there are not so many guys use it).