.NET 4函数:函数参数问题

con*_*att 2 .net c# asp.net-mvc .net-4.0

我正在关注.NET 4框架上的MVC教程.教程创建了这样的函数......

using System.Web;
using System.Web.Mvc;

namespace vohministries.Helpers
{
    public static class HtmlHelpers
    {
        public static string Truncate(this HtmlHelper helper, string input, int length)
        {
            if (input.Length <= length)
            {
                return input;
            }
            else
            {
                return input.Substring(0, length) + "...";
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我不知道this HtmlHelper helper函数参数中的含义或代表.这是.NET 4中的新功能吗?我认为它可能会扩展HtmlHelper类,但我不确定......有人可以解释语法吗?

Mit*_*eat 6

这是一种扩展方法.(从C#3.0开始):

扩展方法使您可以向现有类型"添加"方法,而无需创建新的派生类型,重新编译或以其他方式修改原始类型.扩展方法是一种特殊的静态方法,但它们被称为扩展类型的实例方法.对于用C#和Visual Basic编写的客户端代码,调用扩展方法和实际在类型中定义的方法之间没有明显区别.