Creating an instance of a class containing static void Main ()

I am viewing a console application for C # colleagues and I see this snippet:

class Program { static void Main(string[] args) { Program p = new Program(); p.RealMain(); } ... non-static RealMain function } 

Presumably, he does this because he wants to have instance level fields, etc.

I have not seen this before, but this style scares me. Is this common practice?

+4
source share
8 answers

There is a school of thought that says that the main () function of object-oriented code should do as little as possible. Main () is an "ugly" return to procedural code development, where programs were written in one function, calling subroutines only as needed. In OOP, all code must be encapsulated in objects that perform their tasks when they speak.

So, having done this, you will reduce the LOC at the main () entry point to two lines, and the real program logic is structured and executed in OO mode.

+6
source

That makes sense to me.

In particular, you can add enough logic to Main to parse the command line arguments - perhaps using a generic argument parser - and then pass these parameters to the constructor in a strongly typed way, suitable for the program in question.

Albin asked why this was necessary. In a word: verifiability. In some cases, it is entirely possible to at least test some aspects of a top-level program with unit tests or, possibly, integration tests. Using instance fields instead of static fields (etc.) improves testability here, since you don't have to worry about previous test runs ruining the state.

+5
source

Never seen this before. If you want to go with this template, create a separate Program2 class with RealMain and create an instance instead.

Why do you need instance level fields? Are there enough static fields?

It may be useful if you internally want to instantiate many of the Program classes.

I do not see anything special in this approach, I just did not see it.

+2
source

if you want to get non static functions , you need to do it as follows.

 class Program { static void Main(string[] args) { Program p = new Program(); // dependency of the class will be there.So not a good practice p.RealMain();// if you want initalize, you have to go like this or better you can do it in some other class. } void RealMain(){} } 
+2
source

An application entry point is always defined as static void Main(...) .
You can write your code inside Main () or use this method to run something else elsewhere ... it's up to you ...

+1
source

The accepted practice is to instantiate a separate class that can contain everything you need. The above snippet looks weird, at least :).

+1
source

If it were some other class besides the "Program", the question would not have arisen. This project gives you the opportunity to create multiple instances of the "Program", possibly in the future, so why not. I'm with KeithS here: as little as possible in static void Main.

0
source

I see this a lot, especially for fast console programs, shot down to try something, or check something.

Visual Studio practically encourages it - if you request a new console program, it generates a single file with a class containing only the Main method.

If you are not doing something complicated that requires more than one class or something very simple that does not require a class at all (i.e. all methods and variables are static) why don't you do it this way?

0
source

All Articles