8

Sorry for the unlearned nature of this question. If there's a simple answer, just a link to an explanation will make me more than happy.

After 6 months programming I find static classes to be somewhat useful for storing routines that apply to many different classes. Here's a simplified example of how I use static classes, it's a class for parsing text into various things

public static class TextProcessor 
{
    public static string[] GetWords(string sentence)
    {
        return sentence.Split(' '); 
    }

    public static int CountLetters(string sentence)
    {
        return sentence.Length; 
    }

    public static int CountWords(string sentence)
    {
        return GetWords(sentence).Length; 
    }
}

And I use this in obvious ways like

    class Program
{
    static void Main(string[] args)
    {
        string mysentence = "hello there stackoverflow.";
        Console.WriteLine("mysentence has {0} words in it, fascinating huh??", TextProcessor.CountWords(mysentence)); 

        Console.ReadLine(); 
    }
} 

My question is: Why is it necessary to wrap these static methods in a static class? It seems to serve no purpose. Is there a way I can have these methods on their own not wrapped in a class? I know encapsulation is beneficial but I don't see the use for static methods wrapped in static class. Is there something I am missing stylistically or otherwise? Am I completely barking up a silly tree? Am I thinking too much?

Daniel Mann
  • 57,011
  • 13
  • 100
  • 120
Epic Nobody
  • 189
  • 6
  • Great question, but I suspect it's just a boring "because Java did it." – Jacob Nov 15 '11 at 04:37
  • It ultimately boils down to how the language was designed. All methods have to be in a class. Why? Probably for the sake of consistency. – Daniel Mann Nov 15 '11 at 04:42
  • check this [post][1] [1]: http://stackoverflow.com/questions/7933540/c-sharp-static-class-why-use – xgencoder Nov 15 '11 at 04:44

4 Answers4

7

In C#, any method has to be declared inside a class. That's just how the language is specified.

A static class is actually more akin to a module than a class, so I too think you should be able to either:

  • define a function outside a class or;
  • import a module the same way you import a namespace (with using)

VB.NET, F# and Nemerle actually allow you to declare modules and import them; what allows you to use their methods unqualified.

This is valid Nemerle:

using System.Console; // import static methods in the Console class
class Hello {
  static Main() : void {
    WriteLine("Hello, world!"); // unqualified access!
  }
}

Also, take a look at extension methods, they might allow you to "solve" this in a different way. The methods in your TextProcessor are begging to be string extension methods.

Jordão
  • 55,340
  • 13
  • 112
  • 144
2

This post by eric lippert gives a pretty detailed explanation. I'm not sure if this guy "eric" knows what he's talking about or not though ;-)

Martin Booth
  • 8,485
  • 31
  • 31
1

It would be somewhat awkward to have methods just dangling around in a random namespace.

I suspect the answer is to provide "scope". Just because a method is static, doesn't mean it doesn't have a scope. It can still access other static private methods or member variables - and the class provides a "home" for these things to live in.

Static classes can also have static constructors that get called the first time a static method is used, so this provides the ability to set stuff up as needed.

It's more of an organizational design than anything to due with technical limitations.

Mike Christensen
  • 88,082
  • 50
  • 208
  • 326
  • 2
    "It would be somewhat awkward to have methods just dangling around in a random namespace." - *Most* languages let you do that. – jjnguy Nov 15 '11 at 04:45
  • Most object oriented languages? – Mike Christensen Nov 15 '11 at 04:47
  • I see I see. So in a "hybrid" language like c++ you can have access to functions outside a class but in a rigid OO language like C# you have to encapsulate everything inside a class? .. makes sense. Makes for a lot more boilerplate code than is necessary I think. – Epic Nobody Nov 15 '11 at 04:56
  • Yea I think languages like C++ has to be "backwards compatible" with non-object-oriented C, so they kinda have to support it. VB.NET has to be backwards compatible with BASIC (well, sorta).. I think having classes as a root container for all code is pretty much a standard object-oriented way of thinking. Though I'm sure now people will chime in with "what about language _____" :) – Mike Christensen Nov 15 '11 at 05:01
-3

A static method is a method called in a single instance of a class that is created at run-time.

awiebe
  • 3,758
  • 4
  • 22
  • 33