小编tti*_*tto的帖子

如何验证类中 List<> 属性中的数据?

我现在正在学习面向对象编程,但我在练习中遇到了问题。我有一个有学生的学校班级。同一班级的学生人数不得相同。我有以下内容:

class SchoolClass 
{
    private List<Student> students;

    public List<Student> Students
    {
        get { return this.students; }
        set
        {
            if (value == null) throw new ArgumentNullException("Students list can not be null!");
            if (value.Select(n => n.Number).Distinct().Count() != value.Count())
                throw new ArgumentException("There are students in the list with the same class number!");
            this.students = value;
        }
    }
public SchoolClass( List<Student> students)
    {
        this.Students = students;
    }
}
class Student 
{
    private uint number;

    public uint Number
    {
        get { return this.number; }
        set …
Run Code Online (Sandbox Code Playgroud)

c# oop properties list

3
推荐指数
1
解决办法
2876
查看次数

标签 统计

c# ×1

list ×1

oop ×1

properties ×1