有人可以解释一下C#“ Func T,T>”有吗 [英] Can someone explain what the C# "Func<T,T>" does?

查看:46
本文介绍了有人可以解释一下C#“ Func T,T>”有吗的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读Pro MVC 2的书,并且有一个为HtmlHelper类创建扩展方法的示例。



下面是代码示例:

 公共静态MvcHtmlString PageLinks(此HtmlHelper html,PagingInfopageInfo,Func< int,string> pageUrl)
{
//魔术在这里。
}

这是一个示例用法:

  [测试] 
public void Can_Generate_Links_To_Other_Pages()
{
//安排:我们将扩展Html帮助器类。
//我们使用的变量是否为null无关紧要
HtmlHelper html = null;

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

Func< int,String> pageUrl = i => 页面 + i;

//操作:这是格式化链接的方式。
MvcHtmlString result = html.PageLinks(pagingInfo,pageUrl);

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

}

编辑:删除了混淆该问题要点的部分。





谢谢!

解决方案

A Func< int,string>

  Func< int,String> pageUrl = i => 页面 + i; 

是唯一接受 int 的代表参数并返回字符串。在此示例中,它接受名称为 i int 参数,并返回字符串 Page + i 仅将 i 的标准字符串表示形式连接到字符串 Page



通常, Func< TSource,TResult> 接受一个类型为<$ c $的参数。 c> TSource 并返回 TResult 类型的参数。例如,

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

然后您可以说

  string upper = toUpper( hello,world!); 

  Func< DateTime,int>月= d => d。月; 

所以你可以说

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


I'm reading the Pro MVC 2 book, and there is an example of creating an extension method for the HtmlHelper class.

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? When should I use it? What is Func?

Thanks!

解决方案

A Func<int, string> like

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

is a delegate accepting int as its sole parameter and returning a 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".

In general, Func<TSource, TResult> accepts one parameter that is of type TSource and returns a parameter of type 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));

这篇关于有人可以解释一下C#“ Func T,T&gt;”有吗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆