Method of extending a call to another in the same extension class - good design?

I ask myself if this is a good construct if an extension method is used different in the same extension class.

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; } } 

EDIT Thanks for the answers. And sorry for the poor sampling.

+7
design c # extension-methods
source share
4 answers

It's good. Of course, your example is a little trivial, but consider other situations where the method can provide overload (using the string.Substring method as an example ... the pretend method does not exist yet).

 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 } } 

The call to overload, obviously, allows you to centralize your logic as logically as possible without repeating yourself. This is true, for example, in methods, this is true in static methods (including, by extension, extension methods).

+6
source share

IMHO, as a rule, this is because it reduces the amount of code you have to write, and therefore the ability to make mistakes.

In the above example, however, I find it to be redundant due to the simplicity of the method.

+2
source share

Yes, this is a good practice. Consider the class as a kind of namespace and related extensions together.

+1
source share

Of course, this is a good design, and it can be called DRY .

This, however, is a very trivial example.

0
source share

All Articles