Click here to Skip to main content
15,880,427 members
Please Sign up or sign in to vote.
4.03/5 (6 votes)
See more:
In a C# program,which is the first function to be executed?

[The question is not as trivial as it may seem. It made me thinking for a while: how to illustrate the statement that the entry point method does not have to be the first method executed and how to guarantee that some other method which is not an entry point would be executed before the call to the entry point. I think this is something good to understand for everyone. I provided an answer in Solution 2; will be much grateful if someone provides any essentially different solution. — SA]
Posted
Updated 17-Sep-14 8:25am
v2
Comments
Sergey Alexandrovich Kryukov 17-Sep-14 14:27pm    
Sorry someone voted 1 for this question. The question is not so trivial as it may seem to some. I up-voted it with my 5 and added my edit to it, to attract the attention to this aspect to those who already read posts on this page before.

Thank you for the interesting question.

—SA

The entry point: http://en.wikipedia.org/wiki/Entry_point#C.23[^].

Some say, it has to be the method Main, but strictly speaking, this is not exactly so: pay attention for the .entrypoint IL directive mentioned in the article referenced above.

But this is not so simple: you can have some static declarations which are initialized with calling some methods, which also should be static. In this cases, the order of execution depends on several factors related to those declarations/definitions.

—SA
 
Share this answer
 
Comments
Baranikumar_K 17-Sep-14 13:15pm    
Main
Sergey Alexandrovich Kryukov 17-Sep-14 13:20pm    
No, not exactly. Almost, but not always!
—SA
Sergey Alexandrovich Kryukov 17-Sep-14 14:29pm    
In Solution 2, I provided your some guaranteed illustration of my point. Will you accept it formally, too?
Thank you again for this question.
—SA
CPallini 17-Sep-14 14:05pm    
5.
Sergey Alexandrovich Kryukov 17-Sep-14 14:28pm    
Thank you, Carlo. This is a pretty interesting, not so trivial question. I even provided one more detailed answer with the code sample. I would be interested if you could give us some different idea.
—SA
The question is not as trivial as it may seem. It evokes some trickier question: how to illustrate the statement that the entry point method does not have to be the first method executed and how to guarantee that some other method which is not an entry point would be executed before the call to the entry point.

I think this is the simplest possible solution:
C#
using System;

static class EntryPointRacer { // static is optional in this line

    // static is important
    static EntryPointRacer() { NotEntryPoint(); }

    // static is unavoidable
    static void NotEntryPoint() {
        Console.WriteLine(
            "This string is written before the entry-point method call");
    } //NotEntryPoint

    // entry-point method:
    static void Main(string[] args) {
        Console.WriteLine("This string is written in the entry-point method");
    } //Main

} //class EntryPointRacer


This is complete code. The effect of having a static field or property declaration I mentioned in Solution 1 would work if such member is used in the static constructor. Then the initialization of this static member will be required before the entry-point method call, which can also call a different static method. In this code sample, this technique would be redundant.

I will be much grateful if someone presents any essentially different solution.

—SA
 
Share this answer
 
v5
Comments
Matt T Heffron 17-Sep-14 18:49pm    
Yup!
C# Language Specification section 10.12
+5
Sergey Alexandrovich Kryukov 17-Sep-14 19:16pm    
Thank you, Matt.
—SA
BillWoodruff 17-Sep-14 23:03pm    
+5 for imagination !
Sergey Alexandrovich Kryukov 17-Sep-14 23:29pm    
I knew you would like it. :-)
Thank you, Bill.
—SA
Renju Vinod 18-Sep-14 0:44am    
+5 Thanks for the valuable information.
My solution is The first method/function to be executed in c# is Main()(Provided the class inside which Main() is declared doesn't contain any static constructor). Else the first Method/Function to be executed is the static constructor. (constructor is a special type of Method)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900