user1765862
user1765862

Reputation: 14145

simple constructor using this

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;
 }

Upvotes: 1

Views: 105

Answers (3)

Kim Hansson
Kim Hansson

Reputation: 501

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.

Upvotes: 2

Tilak
Tilak

Reputation: 30688

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

Upvotes: 0

user1610015
user1610015

Reputation: 6668

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

Upvotes: 5

Related Questions