[Introduction to solidity] 7. Initial value of variables

In solidity, variables declared but not assigned have their initial or default values.

value type initial value

  • boolean: false

  • string: “”

  • int: 0

  • uint: 0

  • enum: the first element in the enumeration

  • address:0x00000000000000000000000000000000000000000000(或address (0) )

  • function

    internal: blank equation

    external: blank equation

Verify initial value

You can use the getter function of the public variable to verify whether the initial value written above is correct:

bool public _bool; //false
string public _string; //""
int public _int; //0
uint public _uint; //0
adress public _adress; //0x0000000000000000000000000000000000000000

enum ActionSet { Buy, Hold, Sell}
ActionSet public _enum; //第一个元素0

function fi() internal{} // internal空白方程
function fe() external{} // external空白方程

Reference type initial value

  1. Mapping : A mapping in which all elements have their default values

  2. Structure struct : A structure with all members set to their default values

  3. array :

        **动态数组: []**
    
        **静态数组(定长)**:所有成员设为**其默认值的静态数组**
    
  4. You can use the getter function of the public variable to verify whether the initial value written above is correct:

// Reference Types
uint[8] public _staticArray; // 所有成员设其默认值的静态数组[0,0,0,0,0,0,0,0]
uint[] public _dynamicArray; // '[]'
mapping(uint = > address) public _mapping; // 所有元素都为其默认值的mapping
// 所有成员设为其默认值的结构体0,0
struct Student{
    uint256 id;
    uint256 score;
}
Student public student;

delete operator

delete a will change the value of variable a to the initial value

——You can delete the value of a variable and replace it with the initial value

// delete操作符
bool public _bool2 = true;
function d() external {
    delete _bool2; //delete 会让_bool2变为默认值,false
}

Guess you like

Origin blog.csdn.net/weixin_44792616/article/details/127490421