14

What is the difference between this.balance VS address(this).balance in solidity contracts?

Kashish Khullar
  • 1,563
  • 3
  • 14
  • 26

2 Answers2

17

this represents a "contract instance" object for the current contract. The balance function is part of the "address" object. You used to be able to do this.balance because solidity would allow implicit access to the address functions through the contract instance object.

However, as of Solidity 0.4.22, the recommended practice is to use address(this).balance which explicitly converts this (the current contract instance) to an address object.

In Solidity 0.5.0, this.balance will be explicitly forbidden, and you will be required to explicitly convert to the address object before you can access it's functions.

You may find code using the older syntax due to historical practices, but you should use the modern syntax for any new contracts that you write.

Shawn Tabrizi
  • 8,008
  • 4
  • 19
  • 38
1

I have to use this, but im on the Energi chain

Solidity 0.7.5

return (payable(address(this))).balance;

Kim Ilyong
  • 161
  • 1
  • 3