Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Add methods to Program.cs in .NET 6

In the Program.cs for .NET 5, you could add methods under the Main(string[] args) method. With .NET 6, the Main method exists, but isn’t physically included in the Program.cs file by default. To that end, I’m wondering how you add methods to Program.cs. For example:

// .NET 5
public class Program
{
   static void Main(string[] args)
   {
      // body
   }

   public static void MyMethodHere()
   {
      // method body
   }
}

How would you add the MyMethodHere() class in .NET 6 in Program.cs without manually typing out the whole Program class and Main method?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

This would work as well:

SayHello();

void SayHello()
{
  Console.WriteLine("Hello World");
}

Methods are possible, but without the access modifiers.
The compiler internally creates a static class.


Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading