How to print any value after calling the Main () method?

I have the following code where I print the values ​​before the method Main()is called using the static constructor. How to print a different value after returning the Main () function, without changing the methodMain()

I need a conclusion:

1st 
2nd 
3rd

My "base" code:

class Myclass
{        
    static void Main(string[] args)
    {
        Console.WriteLine("2nd");
    }              
}  

I added a static constructor to Myclass to display "1st"

class Myclass
{     
static Myclass() { Console.WriteLine("1st"); }   //it will print 1st 
    static void Main(string[] args)
    {
        Console.WriteLine("2nd"); // it will print 2nd
    }              
}

Now I need to do the 3rd without changing the methodMain() . How can I do this, if at all possible?

+5
source share
4 answers

Static, AppDomain.ProcessExit , Main() .

class Myclass
    {
        // will print 1st also sets up Event Handler
        static Myclass()
        {
            Console.WriteLine("1st");
            AppDomain.CurrentDomain.ProcessExit += new EventHandler(CurrentDomain_ProcessExit);
        }

        static void Main(string[] args)
        {
            Console.WriteLine("2nd"); // it will print 2nd
        }

        static void CurrentDomain_ProcessExit(object sender, EventArgs e)
        {
            Console.WriteLine("3rd");
        }
    }
+6

, , :

.NET Console

, ? , Main? , ?

Main :

class Myclass
{     
static Myclass() 
    static void Main(string[] args)
    {
        Console.WriteLine("1st");
        Process(args);
        Console.WriteLine("3rd");
    }  

    static void Process(string[] args) {
        Console.WriteLine("2nd"); // it will print 2nd
    }
}
+5

#. , ++, , # . , , .

+3

Add another class with a static one Main, suitable as an entry point to the program. In this call Myclass.Main:

class MyOtherClass {
  static void Main(string[] args) {
    Console.WriteLine("1st");
    Myclass.Main(args);
    Console.WriteLine("3rd");
  }
}

and then change the build options to select MyOtherClassas the entry point to the program. In VS, this is done in Project Properties | Application | Running an object. At the command line, use the option /main:typenamefor `csc.exe.

+3
source

All Articles