In the following constructor, is the ‘this’ keyword required? I know I can remove it, it complies and everything is okay. If I omit ‘this’ will that cause problems for me down the road? Is ommission of ‘this’ considered bad practice?
// Constructor:
public Employee(string name, string alias)
{
// Use this to qualify the fields, name and alias:
this.name = name;
this.alias = alias;
}
No,
thisis purely optional in almost all cases. The only reason it is required in your example is to disambiguate between local variables and parameters and member variables that happened to have the same identifier name.You could avoid the use of
thisby renaming either the parameter or the field to something unique.