Predicate的用法

Predicate 泛型委托

  表示定义一组条件并确定指定对象是否符合这些条件的方法。此委托由Array和List类的几种方法使用,用于在集合中搜索元素。

看看下面它的定义:

    // Summary:
    
//     Represents the method that defines set of criteria and determines whether
    
//     the specified object meets those criteria.
    
//
    
// Parameters:
    
//   obj:
    
//     The object to compare against the criteria defined within the method represented
    
//     by this delegate.
    
//
    
// Type parameters:
    
//   T:
    
//     The type of the object to compare.
    
//
    
// Returns:
    
//     true if obj meets the criteria defined within the method represented by this
    
//     delegate; otherwise, false.
    public delegate bool Predicate<T>(T obj);

类型参数介绍:

   T: 要比较的对象的类型。

   obj: 要按照由此委托表示的方法中定义的条件进行比较的对象。

   返回值:如果 obj 符合由此委托表示的方法中定义的条件,则为 true;否则为 false

 看下面代码:

    public class GenericDelegateDemo
    {
        List
<String> listString = new List<String>()
        {
            
"One","Two","Three","Four","Fice","Six","Seven","Eight","Nine","Ten"
        };
        String[] arrayString 
= new String[] 
        {
             
"One","Two","Three","Four","Fice","Six","Seven","Eight","Nine","Ten"
        };
        
public String[] GetFirstStringFromArray()
        {
            
return Array.FindAll(arrayString, (c) => return c.Length <= 3});
        }
        
public List<String> GetFirstStringFromList()
        {
            
return listString.FindAll((c) => return c.Length <= 3});
        }
        
public String[] GetFirstStringFromArray_1()
        {
            
return ArrayFindAll(arrayString, GetString);
        }
        
public List<String> GetFirstStringFromList_1()
        {
            
return listString.FindAll(GetString);
        }
        
private bool GetString(String str)
        {
            
if (str.Length <= 3)
                
return true;
            
else
                
return false;
        }
    }

(1)首先,上面以 数组和泛型List 两个集合作为演示对象,并构建集合。

(2)接着,两者同时使用各自 所有的 FindALL方法,参见如下定义:

    Array : public T[] FindAll<T>(T[] array, Predicate<T> match);

    List:public List<T> FindAll(Predicate<T> match);

    注意的是,两处的FindAll 均采用了Predicate (泛型委托)作为参数的类型。

(3)接着,使用两者方式展现 对Predicate 的使用:

    第一种:  (c) => { return c.Length <= 3; };

    第二种: GetString(String str)。

这两者在语法上明显不同,但是实际是做相同的事情,第一种是使用Lambda表达式构建的语句

补充的是你也可以这样写:

  delegate(String c){return c.Length<=3;} 作为 Predicate定义的参数

完整代码:

XX.FindAll(delegate(String c) return c.Length <= 3}); 

这应该称为匿名代理了。

其他使用到Predicate 有

  Array.Find , Array.FindAll , Array.Exists , Array.FindLast , Array.FindIndex .....

  List<T>.Find , List<T>.FindAll , List<T>.Exists , List<T>.FindLast , List<T>.FindIndex .....

延伸:

  除了上面提到的外,你完全可以使用Predicate 定义新的方法,来加强自己代码。

public class GenericDelegateDemo
{
    List
<String> listString = new List<String>()
    {
        
"One","Two","Three","Four","Fice","Six","Seven","Eight","Nine","Ten"
    };
    
public String GetStringList(Predicate<String> p)
    {
        
foreach(string item in listString)
        {
            
if (p(item))
                
return item; 
        }
        
return null;
    }
    
public bool ExistString()
    {
        
string str = GetStringList((c) => return c.Length <= 3 && c.Contains('S'); });
        
if (str == null)
            
return false;
        
else
            
return true;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值