Static Method of a Static Class vs. Static Method of a Non-Static Class ( C# )

C#OopStaticMethods

C# Problem Overview


I was asked the above question in an interview. Could you please explain the differences? ( performance - memory - usage - when to use which ? )

Thank you,

Erkan

C# Solutions


Solution 1 - C#

Declaring a static class documents your intent for that class to be a collection of static functionality, and anyone adding instance members will get a compilation error.

A non-static class with static members usually indicates that the class is designed to be instantiated at some point. Static methods of these classes usually do one of two things:

  1. Provide a factory method for creating an instance of that type;
  2. Provide helper functionality that does not require an instance of the type;

Also, as mentioned already, extension methods can only be declared on a static class.

Solution 2 - C#

I assume you were asked for the differences?

A static method on a static class can be used to define an extension method. A static method on a non-static class cannot.

Solution 3 - C#

In terms of performance and memory usage; precisely nothing. Having a static class means you know there are no instances, but back in 1.1 having a private constructor sufficed. Use a static class if it simply makes no sense to have an instance! (utility classes etc)

Solution 4 - C#

When you are providing utility functions and all your methods are static, I recommend you use static methods in a static class.

When you want to provide utility methods that just deal with your instance, I recommend you use static methods in a non-static class. For example:

var myClass = MyClass.Create();
var myClass = MyClass.Parse("serialized.MyClass");

Solution 5 - C#

In terms of memory, there is a slight difference: the static method in a non-static class will be allocated only when the first instance of that type is created, and deallocated when the last instance of that type is deallocated. Static methods on instance objects are very useful when we have collections of objects of the same type in order to decrease the amount of memory used. The drawback of using static methods is that they are not unit testable, so before creating a static method, an eye should be kept on how it will affect the code coverage.

Solution 6 - C#

One major difference I faced when deciding whether to go with normal class with all static methods or, use a static class, is that a normal class supports interface implementation, where as static class does not. I use static class only when I am sure it will be a collection of static functions (usually helper functions), and will never be in the main stream of program. I promote interface programming, for dependency injections, unit testing etc. So, for main flow of program, I use normal class with static methods.

Ref: MS Docs

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionErkan Y.View Question on Stackoverflow
Solution 1 - C#Seth Petry-JohnsonView Answer on Stackoverflow
Solution 2 - C#JaredParView Answer on Stackoverflow
Solution 3 - C#Marc GravellView Answer on Stackoverflow
Solution 4 - C#mythzView Answer on Stackoverflow
Solution 5 - C#Cristina AlboniView Answer on Stackoverflow
Solution 6 - C#PremView Answer on Stackoverflow