Pages

Sunday, 14 June 2015

This Keyword

this Keyword

The this keyword, used in a class, is a reference to the current instance. It can be used only in the blocks of the following class members:
  • Instance constructors.
  • Instance methods.
  • Instance accessors of properties and indexers.
Since static members are not part of an instance, it can not be ussed inside the code of any static function member or in static class.
 public class account
{ 
 private uint accoundID;     //field accountID
 private string accountFirstName;  //field accountFirstName
 private string accountLastName;   //field accountLastName
 private decimal accountBalance;   //field accountBalance
 private static rate;
 
 public account(uint accoundID, string accountFirstName, string accountLastName) 
 {
  this.accoundID = accoundID;     //use this to distinguish between fields and parameters
  this.accountFirstName = accountFirstName; //use this to distinguish between fields and parameters
  this.accountLastName = accountLastName;  //use this to distinguish between fields and parameters
  this.rate = 0.54F;       //compile error, can not access static field with this 
 }
}

0 comments:

Post a Comment