using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Student : MonoBehaviour
{
public string Name { get; set; }
public int Age { get; set; }
public Student(string name, int age)
{
Name = name;
Age = age;
}
public override string ToString()
{
return $"Name: {Name}, Age: {Age}";
}
}
管理类一览
cs复制代码
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
public class StudentManager : MonoBehaviour
{
List<Student> ss;
private void Awake()
{
ss = new List<Student>();
//添加
ss.Add(new Student("张三",10));
ss.Add(new Student("李四", 15));
//遍历
foreach (Student temp in ss)
{
Debug.Log(temp);
}
//查找
string tempName1 = "张三";
//注意下面这行我声明了一个临时的对象存储需要找到对象
//Find可以传入函数所以我就使用了一个lambda表达式
Student foundStudent1 = ss.Find((value)=>value.Name == tempName1);
//其等价于
//students.Find(delegate (Student s) {
// return s.Name == tempName1;
//});
if (foundStudent1 != null) {
Debug.Log($"已找到该学生{foundStudent1}");
}
else
{
Debug.Log($"未找到该学生{tempName1}");
}
}
}