开发C#的程序,写到属性property时,我们可以在Set方法中做一些简单的规则验证:

如下面,Insus.NET写一个Age属性,只允许用户输入10以内的数字:

在属性property做一些简单的验证

 

 class AA
    {
        private int _Age;

        public int Age
        {
            get { return _Age; }
            set
            {
                if (value > 10)
                {
                    throw new Exception("只允许10岁以下的小朋友参加。"); 
                }
                _Age = value;
            }
        }
    }
Source Code

相关文章:

  • 2021-06-05
  • 2021-06-05
  • 2021-12-03
  • 2021-10-15
  • 2022-12-23
  • 2021-09-18
  • 2022-12-23
猜你喜欢
  • 2022-01-19
  • 2022-12-23
  • 2021-07-28
  • 2022-12-23
  • 2021-07-03
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案