1

I'm examing some code and I'm found this bellow snippet of code and I just want to be sure for my understandnig of this second constructor. So, please confirm me is this right understanding of :this()

When User is created with this second constructor it will always inherit assigned Roles property, since Roles property is not assigned anywhere inside second constructor I assume it's left to be used somewhere later in the code.

protected User()
{
    Roles = new HashedSet<Role>();
}

public User(string username, string email, string password, string hashAlgorithm)
   : this()
{
    UserName = username;
    Email = email;
    SetPassword(password, hashAlgorithm);
    IsApproved = true;
 }
user1765862
  • 13,635
  • 28
  • 115
  • 220

3 Answers3

5

The "this()" simply invokes the first contructor. See the last two code snippets in the following MSDN topic:

http://msdn.microsoft.com/en-us/library/ms173115.aspx

user1610015
  • 6,561
  • 2
  • 15
  • 18
2

The syntax : this() will call a constructor that take no arguments, in this case the first constructor in your example. This makes sure that Roles is intitialized in the same manner when calling either constructor.

Kim Hansson
  • 513
  • 4
  • 12
0

Your understanding is correct except that Roles is not inherited and simply other property of the User class.

this() MSDN

When do you use this keyword

Community
  • 1
  • 1
Tilak
  • 30,108
  • 19
  • 83
  • 131