2

I wrote a program which has two main method.And I specified entry point with Command Prompt.

class Program
{
    static void Main()
    {
        Console.WriteLine("Test");
    }
}
class Test
{
    static void Main()
    {
        Console.WriteLine("Test");
    }
}

csc Program.cs /main:Test

Well.Can I determine entry point method with command prompt?

for example

class Program
{
    static void NewEntry()
    {
        Console.WriteLine("Test");
    }
}

Then

csc Program.cs entry/Program::NewEntry()

Is this possible?

Cevizli
  • 153
  • 1
  • 6
  • It must be a static function called `Main` AFAIK. – Lucas Trzesniewski Apr 14 '16 at 07:34
  • 1
    I believe you cannot do so. Entry points are defined by the .NET framework which is Main() on ConsoleApplications – jegtugado Apr 14 '16 at 07:37
  • 1
    Agree with @LucasTrzesniewski but what I really wonder is..Why would you even need to do that? I mean curiosity is one thing but i wonder if you would actually ever need that. – Transcendental Apr 14 '16 at 07:38
  • I used intermediate language.And IL has .entrypoint metadata.I asked this question because maybe C# compiler has option for that.I want to make sure,compiler hasnt this option. – Cevizli Apr 14 '16 at 07:40
  • Well, I believe this would be of no use. You have a single entry point and inside it you can determine the behavior of your application which renders dynamic entry points kind of useless, right? What a unique question btw. – jegtugado Apr 14 '16 at 07:41
  • What are you trying to achieve? If you want to decide at compile time, use a conditional compilation symbol like `#if debug`. If you want to decide at runtime, pass a parameter to the `Main` method and choose alternative paths from there. – Filburt Apr 14 '16 at 07:41
  • Actually I just wondered.I want to make sure "c# always call "Main" for entrypoint and we cant change that." – Cevizli Apr 14 '16 at 07:42
  • Actually, you [can](https://msdn.microsoft.com/query/dev14.query?appId=Dev14IDEF1&l=EN-US&k=k(cs.ProjectPropertiesApplication);k(SolutionItemsProject);k(TargetFrameworkMoniker-.NETFramework,Version%3Dv4.5.2)&rd=true) define the entry point – Panagiotis Kanavos Apr 14 '16 at 07:56
  • @PanagiotisKanavos It still has to be `public static void Main()`. – Filburt Apr 14 '16 at 08:01
  • It doesn't have to be public, it can be any `static void Main` and it *is* possible to have more than one – Panagiotis Kanavos Apr 14 '16 at 08:02
  • 2
    Also it can be static int Main :) But my question is just "Can I define a entry point method without named "Main"" for example I want to "MyMain" for entrypoint method . – Cevizli Apr 14 '16 at 08:04
  • If you want to have multiple entry points of the same code, it's program argument to kick in. – qxg Apr 14 '16 at 08:05
  • If you copied the MSDN links, check yourself. Someone was quick to downvote. As for defining the entry point at *runtime* no - the compiler won't even let you compile the project. The `Main` name is a requirement. For WPF applications you can define different App objects (there is no Main) – Panagiotis Kanavos Apr 14 '16 at 08:06
  • Of course if you compile with `/target:library` you get a bunch of classes with no entry point at all (output will often be called a DLL) instead of an executable (EXE), and then you can always decide later from which method to "start". But I guess that is not what you ask. – Jeppe Stig Nielsen Apr 14 '16 at 08:14
  • Read section 3.1 of the [C# Language Specification](https://msdn.microsoft.com/en-us/library/ms228593.aspx). It's quite clear on the requirements for the entry point. – Damien_The_Unbeliever Apr 14 '16 at 08:29

3 Answers3

1

" When a program starts, it looks for an entry point. This is the role of the Main() method. In fact, a program, that is an executable program, starts by, and stops with, the Main() method. The way this works is that, at the beginning, the compiler looks for a method called Main. If it doesn't find it, it produces an error. If it finds it, it enters the Main() method "

See this document

http://www.functionx.com/csharp2/topics/main.htm

Vincent N.
  • 99
  • 1
  • 12
1

Kind of. The main entry point must be a static Main(). However, you can specify which class to use if there is more than one static Main() in your program.

See the documentation for csc/msbuild on Main Entry Point or Statup Object

In your example, this would be csc -main:MyProject.Program Program.cs where MyProject.Program is the full namespace and name of the entry point class. There are similar flags for msbuild.

In the case you want to dynamically choose an entry point every time the program is run, you probably want to pass that option as a command line argument to a single Main(string[] args) and then branch from there.

TeamBrett
  • 479
  • 1
  • 5
  • 9
-1

No. The static Main() is the only entry point for C#, and all other functions should be called from it.

  • 1
    I think you are right.But can you prove this case? Because I want to be sure. MSIL language have this option for entry point.Maybe csharp have to? And if we cant determine own entrypoint method why? msil has this option.Why C# compiler has not? its not necessary ofcourse,but this is the only reason? Why MSIL need this but csharp not? I hope you understand my point. – Cevizli May 10 '16 at 23:02
  • @Cevizli C# forces this because of the C# language specification saying so (the entry point **must** be named `Main`), hence the compiler enforces that. The CLR may be capable of using other entry points, but the C# language dictates that. – Alejandro Apr 27 '21 at 18:58