Open In App

Difference Between this and address(this) in Solidity

Improve
Improve
Like Article
Like
Save
Share
Report

Solidity is a high-level programming language that is designed for the implementation of a smart contract. Solidity is the language that is in its initial stages and is constantly evolving. There are many breaking changes that have been brought into solidity since its implementation. The current version of solidity is 0.8.13 and till now there have been four breaking changes into solidity.

Some of the breaking changes include:

1. Solidity v0.5.0 Breaking Changes
2. Solidity v0.6.0 Breaking Changes
3. Solidity v0.7.0 Breaking Changes
4. Solidity v0.8.0 Breaking Changes

The breaking changes bring a different approach to an already existing approach with an enhancement. this and address(this) are the same things that have different terminologies due to these breaking changes.

this is the term used for the smart contract when the solidity version is below 0.5.0 .

address(this) is the term used in the latest versions of the solidity to refer to the smart contract.

Let’s understand the difference between the two terms using a smart contract example on Remix IDE.

1. Smart Contract Using this (Solidity version <0.5.0): 
Below is the solidity program to implement the above approach:

Solidity




// Solidity program to implement
// the above approach
// SPDX-License-Identifier: GPL-3.0
pragma solidity <0.5.0;
 
contract old_version
{
    address public myAddress_in_old_version = this;
    uint public myBalance_in_old_verison = this.balance;
     
}


Output: 

Output this

 

You might get a different contract address but it can be observed in the above output that this keyword gives us the contract address and the balance of the contract using the balance function.

Using this keyword in 0.5.0 and above version:

The following error will be shown when trying to use this keyword in the 0.5.0 version and above. 

 

It can be observed that the compiler is providing a suggestion to use address(this) to access the address member.

2. Smart Contract using address(this):
Below is the solidity program to implement the above approach:

Solidity




// Solidity program to implement
// the above approach
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.5.0 <0.9.0;
 
contract new_version
{
    address public myAddress_in_new_version_ = address(this);
    uint public myBalance_in_new_verison = address(this).balance;
}


Output:

Output address(this)

 

You might get a different contract address but it can be seen that the address(this) keyword gives us the contract address and the balance of the contract using the balance function.

From the above examples, it can be concluded that there is no difference between this and address(this) apart from the version changes in Solidity.

S. No.

address(this)

this

1. Used in Solidity Version >=0.5.0 Used in Solidity Version < 0.5.0
2. Brought in at the Breaking Change 0.5.0 It was used initially till the breaking change of 0.5.0

The 0.5.0 breaking changes can also be found in Solidity Documentation:

Solidity Documentation

 



Last Updated : 10 Feb, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads