c# - '+=' 不适用于'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)
{
}
}
}
解决方案
您应该将学生添加到学生列表中,如下所示:
class Classroom
{
List<Student> student = new List<Student>();
int roomNumber;
public void Enrollment(Student item, int n)
{
student.add(item);
}
}
推荐阅读
- c# - 如何使用 C# 在页面对象模型中实现“findelements”
- python - 有没有更快的方法来删除熊猫中数据框的所有空白/标签?
- c - 如何在链表的末尾添加一个节点?
- python - 使用 FiPy 对一维化学工程扩散问题进行建模
- javascript - bootstrap 4 - 导航栏品牌标志在向下滚动时不会改变
- docker - 在 CentOS 上增加 Docker 容器存储大小
- javascript - Vue.js 转换 - 进入和离开,取决于递增/递减值
- python - 为什么熊猫意味着,在日期时间,在一个系列上工作,而不是在一个 groupby 对象上
- javascript - 删除传单地图上的所有标记
- java - 有没有办法让 PowerMockito 类的 Jacoco 代码覆盖率?