Low-level difference: non-static class with static method vs. static class with static method

Go To StackoverFlow.com

13

I was wondering what are the general benefits (or drawbacks) of using a non-static class with a static method versus a static class with the same static method, other than the fact that I cannot use static methods from a non-static class as extension methods.

For example:

class NonStaticClass
{
    public static string GetData()
    {
        return "This was invoked from a non-static class.";
    }
}

Versus this:

static class StaticClass
{
    public static string GetData()
    {
        return "This was invoked from a static class.";
    }
}

What are the performance/memory implications of using one method over another?

NOTE: Suppose that I do not need to instantiate the class. My use-case scenario is limited to something like this:

Console.WriteLine(NonStaticClass.GetData());
Console.WriteLine(StaticClass.GetData());
2012-04-05 18:11
by Den Delimarsky


16

The main benefit is that if you make the class static, the compiler will make sure that your class will only have static members.

So anyone reading the code, will instantly see the class can't be instantiated, and there is no interaction to be considered with any instances of the class. Because there can't be any.

At the clr level, there is no notion of static. A static class is both abstract and sealed, which effectively prevents inheritance and instantiation.

As for performance, I don't see any possiblities for compiler or runtime to optimize one over the other.

In this example, I would focus on expressing your intent as clear as possible to the readers. You can always optimize later.

2012-04-05 18:15
by NoName


3

There are some peculiarity/limitation:

  • you can't instantiate static class
  • you can't inherit static class

So if you suppose such a behavior for you class (e.g. helper/util or extension methods container), if you want to limit it's usage - make it static.

2012-04-05 18:14
by abatishchev
Check my edited post for a use case scenario - I generally was wondering if there is any difference by using it without instantiation - Den Delimarsky 2012-04-05 18:17
Updated the post - Den Delimarsky 2012-04-05 18:21
In addition to the points you mention: you also cannot use a static class as the type of a local variable, field or formal parameter. You cannot use it as the element type of an array. You cannot use it as a type argument in a generic type argument list - Eric Lippert 2012-04-05 18:51
@FishBasketGordo: We've update my answer simultaneously. Please do that again, or post a comment - abatishchev 2012-04-05 21:13
@Dennis: A remark regarding an edit was actual to Fish, by mistake I mentioned you, sorry : - abatishchev 2012-04-05 21:14


3

There are no benefits or drawbacks. It's just architectual decision.

Use static only classes in cases of providing a function set or function libriary. As you can not instantiate it, but you can use it like.

MyMathLibrary.Plot(...)

This types are usually imply state less behavior. I repeat, usually.

Use simple classes with static members when you have "normal" type but need somehow a stateless method, like for example:

public class MyType
{
    .... //some members 

    public static MyType ReadFromXml(XmlReader reader) {}
    public static void SaveToXml(MyType mt) {}
}

There is no a silver bullet, it's just a matter of architect choice.

2012-04-05 18:19
by Tigran
@Dennis Delimarsky: why you say "My use-case scenario is limited to something like...". I don't see it like a limitation. Depends on your architecture it could be really good design choice, by making explicit the behavior of your API - Tigran 2012-04-05 18:26
That is just a sample where I tried to show that I don't intend to actually instantiate the class - Den Delimarsky 2012-04-06 01:23


2

Performance implications: basically none.

You can create an instance of NonStaticClass, but since it has no non-static overriden methods it's pretty much as useful as saying object obj = new object(), which is to say it's useful for maybe locking and that's about it. Making the class static prevent someone from new-ing it, but it's not like that does any real harm.

It's more of a self documenting way of saying there's no reason to create an instance of the class.

2012-04-05 18:14
by Servy
Ads