{

How to get array length in a solidity| Solidity by example


The array is a fixed or dynamic compile time in solidity.

Sometimes, We want to find the array length to check several elements in Solidity.

Solidity Array provides a length property to return the number of array elements in it.

How to find the length of an array in solidity.

First, Array is created by initializing static data. Next, create a size() public function which returns the count of elements in an array. length property returns the number of elements in an array

// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.7;

contract test {
   string[3] numbers=["one","two","three"];


    function size() public view returns(uint){
            return numbers.length;;
    }

 
}

Output:

{
    "0": "uint256: 3"
}
THE BEST NEWSLETTER ANYWHERE
Join 6,000 subscribers and get a daily digest of full stack tutorials delivered to your inbox directly.No spam ever. Unsubscribe any time.

Similar Posts
Subscribe
You'll get a notification every time a post gets published here.