does the "this" operator create implicite objects?


hi friends,

cosnider following class:

public class a
{
 int i;
 decimal j;
 b myb;

 public a()
 {
  i = 15;  //---------> line 1
  this.j = 23.50;  //----------->line 2

  myb = new b(); //-------------->line 3
  myb.count = 2;  //-------------->line 4
   
 }


 
}

public class b
{
 int count;
}

in there have highlighted several ways assign values members.

in line 1, memeber value assign happens through calling member directly

in line2, memeber value assign happens through calling member through current object of class. class creates implicite object first, assign value member through object

in line 4, member value assign happens through calling member through explictely create object of class b

what need know (actually double checking), in line 2, this operator creates internal object of current class?

in line 1, statement assigns member value, without creating object implicitely (internally)?

thanks



no, this-> operator doe not create implicit objects.

in example above, "this->j = 23.5" sets value of j in current instance of class. same "j = 23.5". this-> operator not used in way, because it's redundant, although may used emphasis make clear "j" in case class's property , not local loop counter or whatever.

there few special cases this-> needed, such when class wants pass reference class.


Visual Studio Languages  ,  .NET Framework  >  Visual C#



Comments

Popular posts from this blog

Azure DocumentDB Owner resource does not exist

job syspolicy_purge_history job fail in sqlserver 2008

Trying to register with public marketplace error with 'Get-AzureStackStampInformation'