我正在关注 tutorial on MVC在 .NET 4 框架上。本教程创建了一个像这样的函数...
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) + "...";
}
}
}
}
我不知道这个 HtmlHelper 帮助器
在函数参数中意味着或代表什么。这是 .NET 4 中的新功能吗?我认为它可能会扩展 HtmlHelper 类,但我不确定......有人可以解释语法吗?
最佳答案
这是一个 extension method 。 (从 C# 3.0 开始):
Extension methods enable you to "add" methods to existing types without creating a new derived type, recompiling, or otherwise modifying the original type. Extension methods are a special kind of static method, but they are called as if they were instance methods on the extended type. For client code written in C# and Visual Basic, there is no apparent difference between calling an extension method and the methods that are actually defined in a type.
关于c# - .NET 4函数: Function argument question,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3429154/