我问自己,如果扩展方法使用,这是否是一个好的设计 另一个在同一个扩展类中。
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/