[ACCEPTED]-What is the function of the "this" keyword in a constructor?-constructor

Accepted answer
Score: 15

It calls the other constructor in that class 5 with that signature. Its a way of implementing 4 the constructor in terms of other constructors. base can 3 also be used to call the base class constructor. You 2 have to have a constructor of the signature 1 that matches this for it to work.

Score: 8

this lets you call another constructor of 3 Employee (current) class with (string, int) parameters.

This 2 is a technique to initialize an object known 1 as Constructor Chaining

Score: 3

This sample might help some of the different 19 derivations... The first obviously has two 18 constructor methods when an instance is 17 created... such as

FirstClass oTest1 = new 16 FirstClass(); or FirstClass oTest1b = new 15 FirstClass(2345);

The SECOND class is derived 14 from FirstClass. notice it too has multiple 13 constructors, but one is of two parameters... The 12 two-parameter signature makes a call to 11 the "this()" constructor (of the second 10 class)... Which in-turn calls the BASE CLASS 9 (FirstClass) constructor with the integer 8 parameter...

So, when creating classes derived 7 from others, you can refer to its OWN class 6 constructor method, OR its base class... Similarly 5 in code if you OVERRIDE a method, you can 4 do something IN ADDITION to the BASE() method...

Yes, more 3 than you may have been interested in, but 2 maybe this clarification can help others 1 too...

   public class FirstClass
      int SomeValue;

      public FirstClass()
      { }

      public FirstClass( int SomeDefaultValue )
         SomeValue = SomeDefaultValue;

   public class SecondClass : FirstClass
      int AnotherValue;
      string Test;

      public SecondClass() : base( 123 )
      {  Test = "testing"; }

      public SecondClass( int ParmValue1, int ParmValue2 ) : this()
         AnotherValue = ParmValue2;
Score: 0

A constructor is a special method/function that is 9 ran to initialize the object created based 8 on the class. This is where you run initialization 7 things, as setting default values, initializes 6 members in all ways.

"this" is a special 5 word which points so the very own object 4 you're in. See it as the objects refereence 3 within the object itself used to access 2 internal methods and members.

Check out 1 the following links :

More Related questions