How to define a method following top-level statements
Asked Answered
B

3

18

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");
    }
}
Bicorn answered 8/5, 2022 at 11:3 Comment(0)
L
30

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");
    }
}
Lean answered 8/5, 2022 at 12:22 Comment(0)
L
11

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");
            }
        }
    }
}
Laurenalaurence answered 10/2, 2023 at 14:7 Comment(2)
This isn't quite right, this ThisShouldBeAMethodOfProgramClass method is just a local method that cannot be accessed by other code.Lean
You are right, but he might just use the method ThisShouldBeAMethodOfProgramClass() only for one time.Laurenalaurence
C
0

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.

Cutler answered 18/11, 2023 at 15:57 Comment(1)
As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.Cerracchio

© 2022 - 2024 — McMap. All rights reserved.