c# 泛型和委托

List<T> lst=new List<T>();
List<T> l=lst.FindAll(delegate(T t){return t.name.Contains("a");});
或使用LINQ

 

 

       private void button1_Click(object sender, EventArgs e)
        {
            List
<string> myList = new List<string>();
           
for(int i = 0; i < 10; i++)
            {
                myList.Add(i.ToString ());
//我给他填充1--10
            }

           
if (myList.Find(MathChar) != null)
            {
                MessageBox.Show(
"有这个元素");
            }
           
else
            {
                MessageBox.Show(
"没有这个元素");
            }
        }


       
private bool MathChar(string str)
        {

           
if (str.Equals("3"))//这里来判断是否有3这个元素
                return true;
           
else
               
return false;
        }

 

 

 

 

 

using System.Collections;
using System.Collections.Generic;
using System.Linq;

var list=new List<string>();
list.Where(x=>{
if(x=="")
{
return true;
}
else
{
return false;……

 

 

 

自定义比较器器c#

 class userinfo
        {
            public string userno;
            public string username;
        }
        class UserComparer<T> : IEqualityComparer<T> where T : userinfo
        {

            #region IEqualityComparer<T> Members

            public bool Equals(T x, T y)
            {
                return x.username == y.username;
            }

            public int GetHashCode(T obj)
            {
                return obj.username.GetHashCode();
            }

            #endregion
        }

            List<userinfo> infos = new List<userinfo>();
            infos.Add(new userinfo() { userno = "001", username = "张某" });
            if (infos.Contains(new userinfo() { username = "张某" }, new UserComparer<userinfo>()))
                MessageBox.Show("找到该用户");

 

你可能感兴趣的:(c# 泛型和委托)