Skip to content

Top-level statements do not issue errors with other Main method declarations #31672

@MarkMichaelis

Description

@MarkMichaelis

It's also an error if you combine top-level statements with a declared program entry point method, typically a Main method. In a sense, you can think that one file contains the statements that would normally be in the Main method of a Program class.

The compiler will only issue an error if the type name is Program. You could, for example, have

class HelloWorld
{
  static void Main() {}
}

This will result in a warning, but not an error.

Consider the following rewording:

It's warning if you combine top-level statements with a declared program entry point method, typically a Main method. In a sense, you can think that one file contains the statements that would normally be in the Main method of a Program class. For this reason, you cannot, in fact, declare your own Program class - as this is the name given to the class generated by the C# compiler for top-level statements.


Document Details

Do not edit this section. It is required for learn.microsoft.com ➟ GitHub issue linking.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions