简体   繁体   English

有人可以解释一下 C#“Func<T,T> “ 做?

[英]Can someone explain what the C# "Func<T,T>" does?

I'm reading the Pro MVC 2 book, and there is an example of creating an extension method for the HtmlHelper class.我正在阅读 Pro MVC 2 book,并且有一个为 HtmlHelper 类创建扩展方法的示例。

Here the code example:这里的代码示例:

public static MvcHtmlString PageLinks(this HtmlHelper html, PagingInfo pagingInfo, Func<int,string> pageUrl)
{
    //Magic here.
}

And here is an example usage:这是一个示例用法:

[Test]
public void Can_Generate_Links_To_Other_Pages()
{
    //Arrange: We're going to extend the Html helper class.
    //It doesn't matter if the variable we use is null            
    HtmlHelper html = null;

    PagingInfo pagingInfo = PagingInfo(){
        CurrentPage = 2,
        TotalItems = 28,
        ItemsPerPage = 10
    };

    Func<int, String> pageUrl = i => "Page" + i;

    //Act: Here's how it should format the links.
    MvcHtmlString result = html.PageLinks(pagingInfo, pageUrl);

    //Assert:
    result.ToString().ShouldEqual(@"<a href=""Page1"">1</a><a href=""Page2"">2</a><a href=""Page3"">3</a>")           

}

Edit: Removed part that confused the point of this question.编辑:删除了混淆这个问题的重点的部分。

The question is: Why is the example using Func?问题是:为什么示例使用 Func? When should I use it?我应该什么时候使用它? What is Func?什么是函数?

Thanks!谢谢!

A Func<int, string> like一个Func<int, string>类的

Func<int, String> pageUrl = i => "Page" + i;

is a delegate accepting int as its sole parameter and returning a string .是接受int作为其唯一参数并返回string的委托。 In this example, it accepts an int parameter with name i and returns the string "Page" + i which just concatenates a standard string representation of i to the string "Page" .在这个例子中,它接受一个int与名称参数i ,并返回字符串"Page" + i刚刚串接的标准字符串表示i的字符串"Page"

In general, Func<TSource, TResult> accepts one parameter that is of type TSource and returns a parameter of type TResult .通常, Func<TSource, TResult>接受一个TSource类型的参数并返回一个TResult类型的参数。 For example,例如,

Func<string, string> toUpper = s => s.ToUpper();

then you can say那么你可以说

string upper = toUpper("hello, world!");

or或者

Func<DateTime, int> month = d => d.Month;

so you can say所以你可以说

int m = month(new DateTime(3, 15, 2011));

Func<int, String> means a callback method that takes an int parameter and returns a String as the result. Func<int, String>表示一个回调方法,它接受一个int参数并返回一个String作为结果。

The following expression, which is known as a lambda expression :以下表达式,称为lambda 表达式

Func<int, String> pageUrl = i => "Page" + i;

expands to something like this:扩展到这样的东西:

Func<int, String> pageUrl = delegate(int i)
{
    return "Page" + i;
}

The Func<int, string> line that you are inquiring about is known as a lambda expression.您要查询的Func<int, string>行称为 lambda 表达式。

Func<int, String> pageUrl = i => "Page" + i;

This line can be described as a function that takes an int parameter ( i ) and returns a string "Page" + i ;这一行可以描述为一个函数,它接受一个 int 参数 ( i ) 并返回一个字符串"Page" + i

It can be re-written as:可以重写为:

delegate(int i)
{
    return "Page" + i;
}

Have a blog post on this.有一篇关于此的博客文章。 Using Func you can resolve some of functional discrepancy.使用Func可以解决一些功能差异。 Read here .在这里阅读。

Because the PageLinks method is an Extension Method .因为PageLinks方法是一个扩展方法

In extension method, the first parameter starts with this keyword to indicate that it is an Extension method on the type represented by the first parameter.在扩展方法中,第一个参数以this关键字开头,表示它是第一个参数所代表类型的扩展方法。

The Func<T1, T2> is a delegate which represents a transformation from type T1 to type T2 . Func<T1, T2>是代表从类型T1到类型T2的转换的委托。 So basically, your PageLinks method will apply that transformation to int to produce a string .所以基本上,你的PageLinks方法会将该转换应用到int以生成一个string

Func<T, TResult> : Encapsulates a method that has one parameter and returns a value of the type specified by the TResult parameter. Func<T, TResult> :封装一个方法,该方法具有一个参数并返回由 TResult 参数指定的类型的值。 See this page for more details and examples.有关更多详细信息和示例,请参阅此页面 :-) :-)

I have implemented a where() extension method using Func please have a look...我已经使用 Func 实现了一个 where() 扩展方法,请看...

public static IEnumerable<Tsource> Where<Tsource> ( this IEnumerable<Tsource> a , Func<Tsource , bool> Method )
{

    foreach ( var data in a )
    {
        //If the lambda Expression(delegate) returns "true" Then return the Data. (use 'yield' for deferred return)
        if ( Method.Invoke ( data ) )
        {
            yield return data;
        }
    }
}

You can use it like,你可以像这样使用它

        foreach ( var item in Emps.Where ( e => e.Name == "Shiv" ).Select ( e1 => e1.Name ) )
        {
            Console.WriteLine ( item );
        }

Create your own创建自己的

Func<int,string> myfunc; 

then right click Func to view definition.然后右键单击 Func 以查看定义。 You will see it is a delegate underneith你会看到它是一个代表 underneith

public delegate TResult Func<in T, out TResult>(T arg);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM