小编Tom*_*asz的帖子

ArgumentOutOfRangeException.使用getter,setter - C#

我需要创建一个类Person,有字段: name,surnamesalary.如果salary低于0,我得到例外:

ArgumentOutOfRangeException.使用getter,setter

我尝试过:

public class Employee
{
    public string name { get; set; }
    string surname { get; set; }
    private int salary;
    public int Salary
    {
        get
        {
            return salary;
        }
        set
        {
            if (salary < 0)
            {
                throw new ArgumentOutOfRangeException("salary", "wyplata ma byc wieksza niz 0");
            }
            else
            {
                salary = value;
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

在主要:

Employee tmp = new Employee("michal", "jakowski", -1400);
Run Code Online (Sandbox Code Playgroud)

c# getter setter

-2
推荐指数
1
解决办法
110
查看次数

标签 统计

c# ×1

getter ×1

setter ×1