Appropriate use of the static method

Conceptually, is it appropriate to use a static method (C #) when the method will only accept input data and reformat the input data as output? For example:

public static string FormatString(string inputString){ return "some formatting" + inputString + "Some other formatting"; } 

If I had several such methods, could there be a class of static "utility"?

+7
source share
10 answers

I would agree with the other answers so far that this certainly makes sense a lot of the time.

Sometimes you may need to actually increase flexibility a bit by defining an interface and implementing it using instance methods. This gives you the opportunity to use various methods in your code along the way.

Here is an example of what I mean. Suppose you use this formatString method in some code somewhere that looks like this:

 public void DumpToConsole() { foreach (DataField field in m_fields) { Console.WriteLine(StringUtils.formatString(field.ToString())); } } 

OK that's great. (Actually, this is stupid, but anything — just for illustration!) But you could make this method more flexible if it adopted an interface from which you can have various implementations that provide completely different types of formatting:

 public void DumpToConsole(IFormatter<DataField> formatter = null) { // Perhaps you could specify a default. Up to you. formatter = formatter ?? Formatter<DataField>.Default; foreach (DataField field in m_fields) { Console.WriteLine(formatter.Format(field)); } } 

Then instead of StringUtils , which is a static utility class, this will be just one implementation of the class that offers a way to format a specific type of object (in your case, string objects; in my example, these are imaginary DataField objects).

So this is a very long way of saying it depends. If you want maximum flexibility in the future, perhaps you should consider introducing an interface instead of going with a static helper class.

Note that in my example above, another quite acceptable way to approach the problem could be to accept a delegate Func<T, string> instead of this hypothetical IFormatter<T> interface. In my opinion, this is mainly a stylistic choice. But often, interfaces become more realistic when there are several behaviors that you want to customize; that is, defining methods that accept 3, 4 or more delegates can quickly become cumbersome compared to adopting a single interface.

+3
source

Yes, if you have several static methods that are usually related, combining them into a static utility class is a good idea.

If you are talking about consent, it is also worth noting that the naming conventions in most .NET codes for public members with open source code in Pascal (like FormatString instead of FormatString ) and parameters and fields on camels instead of inputString ).

+3
source

If you do this publicly, then yes, it might be better to make some kind of utility class.

Having said that, I will try to make it as “general purpose" as possible, since otherwise they tend to become inaccessible quickly.

+2
source

Yes, static methods in the way you use them in C # are very similar to the C ++ idea of ​​"free functions." So simple C # has no free functions. Eric Lippert has an interesting post around somewhere why this is so. A static class is used to group the functions of a similar utility and would be appropriate if you had several of them that were similar.

+2
source

Yes, that’s fine, your class will act as a “library” of related functions. Your other options for this would be to either pollute the global namespace with various functions, or create a Singleton that would be unnecessary because there is no "state" to account for ...

+2
source

Yes, you could do it. Or you can create a string extension method.

msdn extension methods

In the above example, I use a string formatter instead of inline concatenation.

 string.Format("some formatting {0} some other formatting", inputString ) 
+2
source

It is that this particular formatting would be useful in several places in your code.

If this made sense only within one specific class, then I would prefer to use a private method (static or instance, it would not affect).

Useful classes are useful. The only thing you should be careful about is not to use them too often. If most of your code is in utility classes, then you are doing something wrong. But factorizing some common code in helper methods is a perfectly justifiable use.

+2
source

You can use the extension method to do this to extend the String class. This will make the call code a little neat, but in the end it is just a matter of personal taste.

  public static string MyWeirdFormat(this string str) { return string.Format("{0} is weird",str); } public static void Test() { string myString = "ABCD"; string weirdString = myString.MyWeirdFormat(); } 
+2
source

In my opinion, the answer is yes, you would put these methods in the Utility (Util) class. On the Java web application I am currently working on, there are actually 3 such Util classes, each of which contains only static methods similar to the ones you showed. The reason we have 3 is one for client-only Util methods, one for the server and one for general Util methods.

Depending on your application, you might get something like this.

As an aside, if you want to know more about when to use static classes in C #, look here .

I hope this answers your question enough.

+2
source

Personally, I am more inclined to use the extension method, still static;)

 public static class StringExtensions { public static string MyFormat(this string input) { return string.Format("some formatting {0} Some other formatting", input); } } 
+1
source

All Articles