首页 > 解决方案 > '+=' 不适用于'Student' 和'Student' 类型的操作数

问题描述

我只是在练习一些代码,我遇到了这样的错误。不知道,为什么!也许有人可以帮助我。

问题:首先我想创建学生(学生班级)并将学生添加到教室中。

namespace CollectionTest
{

    class Student
    {
        private string name;
        private int age;

        public void add(string n, int a)
        {
            name += n;
            age += a;
        }
    }

    class Classroom
    {
        Student student;
        int roomNumber;

        public void Enrollment(Student list, int n)
        {
            student += list; //error ('+=' con not be applied to operands of type 'Student' and 'Student')
        }

    }



    class Program
    {

        static void Main(string[] args)
        {
        }
    }
}

标签: c#

解决方案


您应该将学生添加到学生列表中,如下所示:

  class Classroom
    {
        List<Student> student = new List<Student>();
        int roomNumber;

        public void Enrollment(Student item, int n)
        {
            student.add(item);
        }

    }

推荐阅读