How can i implicitly convert my class to another type?

后端 未结 3 644
自闭症患者
自闭症患者 2021-01-19 05:42

For example implicitly

MyClass myClass = new MyClass();
int i = myClass;
3条回答
  •  长发绾君心
    2021-01-19 06:07

    You need to define this in the MyClass file.

    public static implicit operator int(MyClass instance) 
    {
        if (instance == null) 
        {
            return -1;
        }
        return instance._underlyingValue;
    }
    

提交回复
热议问题