0

I currently am working on a project(Console Application) that consists of a number of classes. None of these classes contain any properties. They just contain a number of methods.

Example:

Say I have 2 classes, one is Program.cs which has the main method. The other class, Worker.cs has all the methods that provide the necessary functionality.

Worker.cs just has a number of methods, nothing else.

Presently, in the main function I am calling the methods using the class name :

Worker.method1(); Worker.method2(param1, param2);

My question is : Should I create an object of the class and call it? Or should I call the methods on the class itself?

Is there any affect on performance when using a static class over a non-static one?

There is no information specific to an object that I need to keep track off.

spdcbr
  • 305
  • 3
  • 5
  • 15
  • If worker is actualy an entity of the program that you use quite a lot for the different methods, I would make an instance of the class. When you're just using one method of this class, I agree more in using a static class. All has to do with the role the class plays in your program, I think. – Programmer1994 Apr 12 '15 at 11:51
  • If your class is absolutely stateless and you do not have any need for more complex scenarios (like stateless instance classes for dependency injection) then leave this class static. If you are interested in performance considerations then read http://stackoverflow.com/questions/12279438/performance-of-static-methods-vs-instance-methods. – Eugene Podskal Apr 12 '15 at 11:52
  • See http://stackoverflow.com/questions/12279438/performance-of-static-methods-vs-instance-methods – Murdock Apr 12 '15 at 12:11
  • It just doesn't matter at all at runtime, the CLR has no notion of "static classes". It is purely C# syntax sugar. It is simply an aid to help you avoid adding instance members and for the client programmer to create an instance of the class. When you spend enough time thinking about the design of your program then you'll always get this right. – Hans Passant Apr 12 '15 at 13:07

1 Answers1

0

You only need a class if you intent to keep some state that is not shared across all instances of the class.

If there is no information to keep because of or related to the execution, there is no point in creating an instance, unless you want to use this class as reference for dependency injection or for polymorphism / deriving.

Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325