在C#程序中,这是第一个要执行的函数? [英] In a C# program,which is the first function to be executed?

查看:86
本文介绍了在C#程序中,这是第一个要执行的函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C#程序中,这是第一个要执行的函数吗?



[问题并不像看起来那么微不足道。这让我思考了一段时间:如何说明入口点方法不必是第一个执行的方法以及如何保证其他一些不是入口点的方法的说法在调用入口点之前执行。我认为这对每个人都有好处。我在解决方案2中提供了答案;如果有人提供任何本质上不同的解决方案,将非常感激。 - SA]

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]

推荐答案

入口点: http://en.wikipedia.org/wiki/Entry_point#C.23 [ ^ ]。



有人说,它必须是方法 Main ,但严格来说,情况并非如此:请注意上面引用的文章中提到的 .entrypoint IL指令。



但这并不是那么简单:你可以有一些静态声明,它们通过调用一些方法进行初始化,这些方法也应该是静态的。在这种情况下,执行顺序取决于与这些声明/定义相关的几个因素。



-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


这个问题并不像看起来那么微不足道。它引出了一些棘手的问题:如何说明入口点方法不必是第一个执行的方法以及如何保证以前执行的其他方法不是入口点的语句对入口点的调用。



我认为这是最简单的解决方案:

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:
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





这是完整的代码。如果在静态构造函数中使用了这样的成员,那么在解决方案1中提到的静态字段或属性声明的效果将起作用。然后在入口点方法调用之前需要初始化此静态成员,这也可以调用不同的静态方法。在这个代码示例中,这种技术将是多余的。



如果有人提出任何本质上不同的解决方案,我将非常感激。



-SA


我的解决方案是第一个在c#中执行的方法/函数是Main()(提供了Main里面的类)声明的()不包含任何静态构造函数)。否则,要执行的第一个方法/函数是静态构造函数。 (构造函数是一种特殊类型的方法)
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)


这篇关于在C#程序中,这是第一个要执行的函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆