INT的C#setters

fun*_*err -3 c# int setter

正如我已经做过的那样,我有这段代码:

 private string opt;// create a property
        public string optionInterval
        {
            get
            {
                return opt;
            }
            set
            {
                opt = value;
            }
        }
Run Code Online (Sandbox Code Playgroud)

如何将opt更改为整数?特别是在设定部分?

Joe*_*Joe 6

public int optionInterval { get; set; }
Run Code Online (Sandbox Code Playgroud)

使用自动实现的属性


这是GianT971答案的工作版本:

    private int opt; 
    public string optionInterval
    {
        get
        {
            return opt.ToString();
        }
        set
        {
            opt = Convert.ToInt32(value);
        }
    }
Run Code Online (Sandbox Code Playgroud)


Gia*_*971 5

        private int opt; 
        public string optionInterval
        {
            get
            {
                return opt.ToString();
            }
            set
            {
                opt = Convert.ToInt32(value);
            }
        }
Run Code Online (Sandbox Code Playgroud)