c# - .NET 4函数: Function argument question

标签 c# .net asp.net-mvc .net-4.0

我正在关注 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/

相关文章:

c# - 是否可以将一个巨大的 WebForms 网站移动到 MVC?

c# - 在 C# 中实现只读可哈希集

c# - C# 中的优化技术

c# - 合并两个字符串(文本文件)

c# - 屏蔽电话号码的文本框 View

c# - 自定义授权属性仅适用于本地主机

.net - 替代在 ASP.Net MVC View 中使用 OnLoad 事件?

c# - 如何在相距 10 个元素的数组中查找重复项/重复项

c# - EF NotMapped 和 JsonIgnore 属性导致代码优先问题

asp.net-mvc - 是否可以跨 ASP.NET MVC 框架覆盖 UrlHelper.GenerateUrl()?