C# 有人能解释一下“C”是什么意思吗;Func<;T、 T>&引用;做

C# 有人能解释一下“C”是什么意思吗;Func<;T、 T>&引用;做,c#,func,C#,Func,我正在阅读Pro MVC 2的书,这里有一个为HtmlHelper类创建扩展方法的示例 下面是代码示例: public static MvcHtmlString PageLinks(this HtmlHelper html, PagingInfo pagingInfo, Func<int,string> pageUrl) { //Magic here. } public static MvcHtmlString页面链接(此HtmlHelper html、PaginInfo

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

下面是代码示例:

public static MvcHtmlString PageLinks(this HtmlHelper html, PagingInfo pagingInfo, Func<int,string> pageUrl)
{
    //Magic here.
}
public static MvcHtmlString页面链接(此HtmlHelper html、PaginInfo PaginInfo、Func pageUrl)
{
//这里有魔法。
}
下面是一个示例用法:

[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>")           

}
[测试]
public void可以生成链接到其他页面()
{
//安排:我们将扩展Html助手类。
//我们使用的变量是否为空并不重要
HtmlHelper html=null;
PaginInfo PaginInfo=PaginInfo(){
CurrentPage=2,
总计项目=28,
ItemsPerPage=10
};
Func pageUrl=i=>“页面”+i;
//Act:下面是如何设置链接的格式。
MvcHtmlString result=html.PageLinks(pagininfo,pageUrl);
//断言:
result.ToString().ShouldEqual(@“”)
}
编辑:删除了混淆此问题要点的部分

问题是:为什么示例使用Func?我应该什么时候用?什么是Func


谢谢

因为
PageLinks
方法是一个

在extension method中,第一个参数以this关键字开头,表示它是第一个参数表示的类型上的扩展方法

Func
是一个委托,表示从类型
T1
到类型
T2
的转换。因此,基本上,您的
PageLinks
方法将该转换应用于
int
,以生成
字符串

Func
意味着一个回调方法,它接受
int
参数并返回一个
字符串

以下表达式称为a:

Func pageUrl=i=>“Page”+i;
扩展到如下内容:

Func<int, String> pageUrl = delegate(int i)
{
    return "Page" + i;
}
Func pageUrl=delegate(int i)
{
返回“Page”+i;
}
您要查询的
Func
行称为lambda表达式

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

Func<int, String> pageUrl = i => "Page" + i;
那么你可以说

string upper = toUpper("hello, world!");
int m = month(new DateTime(3, 15, 2011));

Func
:封装具有一个参数的方法,并返回由TResult参数指定的类型的值。和示例。:-)

我已经用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;
        }
    }
}

有一篇关于这个的博客文章。使用
Func
可以解决一些功能差异。阅读。

创建自己的

Func<int,string> myfunc; 
Func-myfunc;
然后右键单击“函数”以查看定义。您将看到它是Neith下的一个委托

public delegate TResult Func<in T, out TResult>(T arg);
public委托TResult Func(T参数);

主要是,我想知道Func pageUrl=I=>“Page1”+I;我不明白,线路在走
Func
与扩展方法无关。你在问什么?你是说
PageLinks
的声明?第一个参数是this
,即它是一个扩展方法。该参数绑定到调用中的
html
对象。注意:请忽略扩展方法位,我写错了问题。我主要想了解Func是什么,以及为什么这个示例选择使用它。还有,我应该什么时候用它。我提供的示例仅用于上下文目的。注意:对扩展方法的引用纯粹是因为在MVC书籍中,Func是在介绍它们的章节中首次使用的。好吧-但是Func部分呢?请参阅我的答案,以获取指向Func的链接。是否可以重载委托,例如,如果输入是一个通用的T where T:class?
int m = month(new DateTime(3, 15, 2011));
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;
        }
    }
}
        foreach ( var item in Emps.Where ( e => e.Name == "Shiv" ).Select ( e1 => e1.Name ) )
        {
            Console.WriteLine ( item );
        }
Func<int,string> myfunc; 
public delegate TResult Func<in T, out TResult>(T arg);