Refer to Module Initializer documentation, the ModuleInitializer
is a useful attribute, which enable your program run something as it initialized.
Usage of this attribute is very simple, just have a class, and a parameterless static method that returns void, and simply give it an attribute.
using System.Runtime.CompilerServices;
namespace LanguageLab;
public class ToBeInitialized
{
[ModuleInitializer]
public static void SayHello()
{
Console.WriteLine("Hello!");
}
}
And then when you run your application, even if there’s no any code in Main
method, but the string Hello!
will be still printed.