Is it possible to apply attribute to the generated main method in a top-level application?

480 Views Asked by At

C#9 supports top-level statements, but I am curious whether it is possible to apply any attribute to generated main method (STAThread, actually), or I have to use classical approach with Main method.

1

There are 1 best solutions below

0
JL0PD On

This feature was designed for newcomers to language so they won't need to write bunch of boilerplate each time. So this

namespace HelloWorldProg
{ 
      public static class HelloWorldClass 
      {  
           public static void Main(string[] args) 
           { 
              System.Console.WriteLine("Finally I can write Hello World");  
           } 
       }  
}

transforms to this

System.Console.WriteLine("That's much easier!");

It's a question of entry threshold and learning curve. Without top-level statement you need to know about

  1. namespaces
  2. classes
  3. incapsulation
  4. static/instance members
  5. passing arguments
  6. arrays
  7. how to write text to console

While with top-level statements you need to know only about last item to be able to write program and you may dig into other themes latter.

It's like "how to write 'hello world' in Haskell". Well, you need to know monads, IO in particular and do-notation. In order to know monads you should learn category theory.

Now answering your question: You cannot declare attributes with top-level statements. They were designed for different purposes. Proposal, priorities in platform design