If a class that will only run once contains a static constructor?

I am starting to learn OOP programming with C #. In terms of design, it makes sense static

for me to use a constructor for the main class of my program, given that this class contains code that will only be run once (my whole program is very simple and consists of a single .cs file).

For example, here's a sample code using a regular constructor:

class Program
    {
        const string file = @"C:\Program Files (x86)\myapp\log.txt";
        int status;

        static int Main(string[] args)
        {
            var myObj = new Program();
            return myObj.status;            
        }

        public Program()
        {
            int retCode;

            try {
                //  lots of procedures using the file

                retCode = 0;   // ok
            }
            catch (Exception ex) {
                Console.WriteLine(ex.Message);

                retCode = 999; // specific error
            }

            status = retCode;
        }
    }

      

The same structure follows here, but using the constructor static

seems to me to be adequate. The notice has status

also been changed.

class Program
    {
        const string file = @"C:\Program Files (x86)\myapp\log.txt";
        static int status;

        static int Main(string[] args)
        {
            return Program.status;
        }

        static Program()
        {
            int retCode;

            try {
                //  lots of procedures using the file

                retCode = 0;
            }
            catch (Exception ex) {
                Console.WriteLine(ex.Message);

                retCode = 999;
            }

            status = retCode;
        }
    }

      


Question: Is my guess correct using the second code instead of the first? Or am I missing something? In other words: which one is preferable (considered the best design)? And is there something fundamental to static constructors that might cause me problems in this case?

+3


source to share


3 answers


Try to avoid using static constructors as much as possible. Unlike instance constructors, you cannot actively reference a static constructor - it fires when the type is first used (which might change due to optimization or even obfuscation).

Also try to avoid "working" in the constructor. The constructor is meant to create an instance and nothing more.



So in both cases, I have moved the functionality to the method. Then this method can have an actual return value rather than setting a property. Since you don't maintain any state ( Program.status

then converted to a return value), you can safely make this method static.

+3


source


The code above does the same except for the first instance you create for the object Program

instead of using static members. This will create additional overhead for your application. Now, in this case, there really is no reason to choose one of the methods as the overhead is negligible.



However, in the first case, the value status

is instance-based. Therefore, only the instance Program

has a status value. So if it status

is an instance based field where multiple instances Program

must support their own field value status

, then you should use the first example.

+1


source


The OP is probably looking for the Singleton pattern. My personal opinion is that if a class is ever to be instantiated once, then there is very little reason for it to be a class in the first place, and the overhead is less if all the methods of such a class were returned to a static C function - but that's just my opinion.

+1


source







All Articles