简体   繁体   中英

Static or non-static class?

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM