C# 中list判断是否有符合条件的对象,可以不用for循环或者foreach,使用自带Api List.Exists
using System;
using System.IO;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.UI;
public enum typea
{
p1,
p2,
p3,
p4
}
public class testModel
{
public int age;
public string name;
}
public class DemoTest : MonoBehaviour
{
List<int> listNum = new List<int>{1,2,3,4};
List<typea> listEnum = new List<typea>(){typea.p1, typea.p2, typea.p3};
List<testModel> tms = new List<testModel>();
// Start is called before the first frame update
void Start()
{
//基础类型
var bContain = listNum.Exists(t => t == 2);
Debug.LogError($"基础类型:{bContain}");
//枚举类型
var bContain1 = listEnum.Exists(t => t == typea.p4);
Debug.LogError($"枚举类型:{bContain1}");
//引用类型
testModel tm = new testModel();
tm.age = 25;
tm.name = "xiaoming";
tms.Add(tm);
var bContain2 = tms.Exists(t => t.name == "xiaoming");
Debug.LogError($"引用类型:{bContain2}");
}
// Update is called once per frame
void Update()
{
}
}