RE: RE: Solidity开发指南
You are viewing a single comment's thread from:

RE: Solidity开发指南

Words
145
Reading
1 min
Listen
Play
4M

delete操作符可以用于任何变量,将其设置成默认值。
删除字符串时,会将其值重置为空。
删除枚举类型时,会将其值重置为序号为0的值。
如果对动态数组使用delete,则删除所有元素,其长度变为0。
如果对静态数组使用delete,则重置所有索引。
如果对map类型使用delete,什么都不会发生。
如果对map类型中的一个键使用delete,则会删除与该键相关的值。

eg:
uint256 public number = 20;
address[] public addrs;

delete number; 
//number = 0;
delete addrs[1];
//addrs[1] = address(0); delete将对应数组中的元素重置为0地址。

contract DeleteDemo{
    bool public b  = true;
    uint public i = 1; 
    address public addr = msg.sender;
    bytes public varByte = "123";
    string  public str = "abc";
    enum Color{RED,GREEN,YELLOW}
    Color public color = Color.GREEN;
    
    function deleteAttr() public {
        delete b; // false
        delete i; // 0
        delete addr; // 0x0
        delete varByte; // 0x
        delete str; // ""
        delete color;//Color.RED
    }
}
@lemooljiang: | Ecency