04/19 : Func Sample Code
/****************************************************************************
*
*
* Author : Dayang Sun
*
* File : Program.cs
*
* Date : 4/19/2010 5:00:49 PM
*
* 使用Func<T,TResult>和Action<T>,Action而不使用Delegate其实都是为了简化代码, 使用更少的代码达到相同的效果,不需要我们显示的声明一个委托,Func<T,TResult>的最后一个参数始终是返回类型,而 Action<T,TResult>是没有返回类型的,而Action是没有返回类型和参数输入的。
*
* ***************************************************************************/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
delegate int Searcher(string searchString, int start, int count,
StringComparison type);
class Program
{
// Without using Func generic deletegate
static void Main(string[] args)
{
//WithoutFunc();
//WithFunc();
//WithFuncAnonymous();
//WithFuncLamada();
WithFuncLing();
}
// Without using Func generic deletegate
static void WithoutFunc()
{
string title = "The House of the Seven Gables";
int position = 0;
Searcher finder = title.IndexOf;
do
{
int characters = title.Length - position;
position = finder("the", position, characters,
StringComparison.InvariantCultureIgnoreCase);
if (position >= 0)
{
position++;
Console.WriteLine ( String.Format("'The' found at position {0} in {1}.",
position, title) + "\n");
}
} while (position > 0);
}
// By this way, can not add more code except call indexof function
static void WithFunc()
{
string title = "The House of the Seven Gables";
int position = 0;
Func<string, int, int, StringComparison, int> finder = title.IndexOf;
do
{
int characters = title.Length - position;
position = finder("the", position, characters,
StringComparison.InvariantCultureIgnoreCase);
if (position >= 0)
{
position++;
Console.WriteLine(String.Format("'The' found at position {0} in {1}.",
position, title) + "\n");
}
} while (position > 0);
}
static void WithFuncAnonymous()
{
string title = "The House of the Seven Gables";
int position = 0;
Func<string, int, int, StringComparison, int> finder =
delegate(string s, int pos, int chars, StringComparison type)
{
Console.WriteLine("sdy");
return title.IndexOf(s, pos, chars, type); };
do
{
int characters = title.Length - position;
position = finder("the", position, characters,
StringComparison.InvariantCultureIgnoreCase);
if (position >= 0)
{
position++;
Console.WriteLine(String.Format("'The' found at position {0} in {1}.",
position, title) + "\n");
}
} while (position > 0);
}
static void WithFuncLamada()
{
string title = "The House of the Seven Gables";
int position = 0;
Func<string, int, int, StringComparison, int> finder =
(s, pos, chars, type) =>
{
Console.WriteLine("WithFuncLamada");
return title.IndexOf(s, pos, chars, type);
};
do
{
int characters = title.Length - position;
position = finder("the", position, characters,
StringComparison.InvariantCultureIgnoreCase);
if (position >= 0)
{
position++;
Console.WriteLine(String.Format("'The' found at position {0} in {1}.",
position, title) + "\n");
}
} while (position > 0);
}
static void WithFuncLing()
{
Func<String, int, bool> predicate = (str, index) => str.Length == index;
String[] words = { "orange", "apple", "Article", "elephant", "star", "andty" };
IEnumerable<String> aWords = words.Where(predicate).Select(str => str);
foreach (String word in aWords)
Console.WriteLine ( word + "\n");
}
}
}
0 Comments:
Post a Comment
<< Home