博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

Func<T1, TResult> 委托

Posted on 2009-09-30 11:17  jowo  阅读(437)  评论(0编辑  收藏  举报
1.Func<T1, TResult> 用法以及含义
   封装一个具有T参数并返回 TResult 参数指定的类型值的方法。如果不需要使用返回TResult 请使用Action<T>
2.如果要使用需要引用System.Core
3.语法格式为:
   public delegate TResult Func<T1, TResult>(
    T1 arg1,
     )

类型参数说明

T1
此委托封装的方法的第一个参数类型。
TResult

此委托封装的方法的返回值类型。

using System;

delegate string[] ExtractMethod(string stringToManipulate, int maximum);

public class DelegateExample
{
   public static void Main()
   {
      // Instantiate delegate to reference ExtractWords method
      ExtractMethod extractMeth = ExtractWords;
      string title = "The Scarlet Letter";
      // Use delegate instance to call ExtractWords method and display result
      foreach (string word in extractMeth(title, 5))
         Console.WriteLine(word);
   }

   private static string[] ExtractWords(string phrase, int limit)
   {
      char[] delimiters = new char[] {' '};
      if (limit > 0)
         return phrase.Split(delimiters, limit);
      else
         return phrase.Split(delimiters);
   }
}