How to define a method following top-level statements

5.9k Views Asked by At

I recently updated Visual Studio and found out about this new feature (to me it is new) of top-level statements.

As I understand it, the compiler completes the definitions for the Program class and Main method, without you having to explicitly type it up.

This is useful, but I'm having trouble when defining a new method. I would like a method in the Program class. And call this with a top-level statement. Here is some example code:

Console.WriteLine("toplevel");
ThisShouldBeAMethodOfProgramClass();

public static void ThisShouldBeAMethodOfProgramClass()
{
    Console.WriteLine("Static in Program class");
}

This is giving me build errors, because the public static modifiers are not valid. I think it interprets this as a local function in Main. I can remove the modifiers, but this is just example code, my real code has more methods and classes.

How can I do this? Should I not use top-level for this?

I would like this to effectively be the same as:

class Program
{
    public static void Main(string[] args)
    {
        Console.WriteLine("toplevel");
        ThisShouldBeAMethodOfProgramClass();
    }
    public static void ThisShouldBeAMethodOfProgramClass()
    {
        Console.WriteLine("Static in Program class");
    }
}
3

There are 3 best solutions below

0
On BEST ANSWER

You can keep using top-level statements and append additional members with a partial Program class.

using System;
Console.WriteLine("toplevel");
ThisShouldBeAMethodOfProgramClass();

public static partial class Program
{
    public static void ThisShouldBeAMethodOfProgramClass()
    {
        Console.WriteLine("Static in Program class");
    }
}
1
On

Top-level statements such as entry point or main() function but there are some notes between them

You can use methods in the Top-level statement in this way:

DisplayMessage();

static void DisplayMessage()
{
    Console.WriteLine("Welcom in Top-Level Statement");
}

Top-level statements enable quick experimentation and beginner tutorials.

2
On

Or just remove the access modifier: method without access modifier

using System;
Console.WriteLine("toplevel");
ThisShouldBeAMethodOfProgramClass();

static void ThisShouldBeAMethodOfProgramClass()
{
    Console.WriteLine("Static in Program class");
}

Update: as per the comment by @shingo, it's not a method without an access modifier which leads to a public method implicitly, but a local function in the invisible Main() method scope which prevents you from invoking it from outside of the file(the whole file with top-level statements is inside the Main()).

So all the above is identify with following:

using System;

namespace Program
{
    public class Program
    {
        public static void Main(string[] args)
        {
            Console.WriteLine("toplevel");
            ThisShouldBeAMethodOfProgramClass();

            static void ThisShouldBeAMethodOfProgramClass()
            {
                Console.WriteLine("Static in Program class");
            }
        }
    }
}