c# - 扩展方法在同一个扩展类中调用另一个 - 好的设计?

标签 c# extension-methods

我问自己,如果扩展方法使用,这是否是一个好的设计 另一个在同一个扩展类中。

public class ClassExtensions
{
   public static bool IsNotNull<T>(this T source)
      where T : class
   {
      return !source.IsNull();
   }

   public static bool IsNull<T>(this T source)
      where T : class
   {
      return source == null;
   }
}

编辑 感谢您的回答。 很抱歉样本不好。

最佳答案

没关系。当然,您的示例有点微不足道,但请考虑方法可以提供重载的其他情况(使用 string.Substring 作为示例......假装方法尚不存在)。

public static class Foo
{
    public static string Substring(this string input, int startingIndex)
    {
         return Foo.Substring(input, startingIndex, input.Length - startingIndex);
         // or return input.Substring(startingIndex, input.Length - startingIndex);
    }

    public static string Substring(this string input, int startingIndex, int length)
    {
         // implementation 
    }
}

显然,调用重载可以使您的逻辑尽可能集中,而无需重复自己。在实例方法中是这样,在静态方法中也是这样(包括,通过扩展,扩展方法)。

关于c# - 扩展方法在同一个扩展类中调用另一个 - 好的设计?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3729727/

相关文章:

c# - 使用 OWIN 启动多个 Web 服务器

c# - 使用 Selenium 等待帧加载

c# - 方法 C# 的扩展方法

c#-4.0 - 扩展方法可以修改扩展类值吗?

c# - 模糊匹配与阈值过滤器 C#

c# - ASP.NET:如何使用客户端 AJAX 脚本调用非静态页面方法?

c# - '$' 字符串前缀不用于插值时意味着什么?

c# - 两个列表的补充?

c# - 如何比较可空类型?

apache-flex - 是否可以在不继承类的情况下向非动态 ActionScript 3 类添加行为?