22

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?

ProgrammingLlama
  • 36,677
  • 7
  • 67
  • 86
AliveInTheCircuits
  • 223
  • 1
  • 2
  • 6

3 Answers3

18

You just type out the method that you require and then call it!.

Console.WriteLine("Hello, World from Main!");

MyMethodHere();

static void MyMethodHere()
{
    Console.WriteLine("MyMethodHere says hello World!");
}

But you can still type it out in full the same as you done before.

What I have done several times for simple console programs is to create the project with .net 5, this then generates using the "old" template, and then I just update TargetFramework it to .net 6 before I do any coding.

See also the guide from MS: https://learn.microsoft.com/en-gb/dotnet/core/tutorials/top-level-templates

jason.kaisersmith
  • 8,712
  • 3
  • 29
  • 51
  • 1
    I appreciate this info for sure, but I was specifically looking to do it in the form of .NET 6. That is to say, WITHOUT actually having to add in that Program class and Main method at all; manually or otherwise. Turns out, it was the access modifier on the method itself which was my issue. Your example and another comment both pointed that out. So thanks! – AliveInTheCircuits Mar 09 '22 at 06:50
  • @AliveInTheCircuits the second part of the answer addresses your automatic concerns and as pointed out, is actually Microsoft’s recommendation too –  Mar 09 '22 at 10:24
  • @jason.kaisersmith What about access modifiers like private and public? When I add them I get a compiler error. Does that all mean that methods added are all by default private to the Program class? – Oliver Nilsen Jun 11 '22 at 20:40
  • @OliverNilsen If you require access modifiers, then you can't do it this way, you have to type it out in full using the "old" method. – jason.kaisersmith Jun 12 '22 at 15:59
12

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.


  • Thanks so much! That was exactly what I was looking for. I didn't realize the access modifier was the issue. So does it make it private or public then by default if you can't add the access modifier? – AliveInTheCircuits Mar 09 '22 at 06:48
5

.NET 6 has this new feature that allows minimal main. They unfortunately use it by default in new console projects. Its very confusing. Anyway you can write the old main just like you used to. You just have to type out all the code, or cut and paste from an old project

Guru Stron
  • 102,774
  • 10
  • 95
  • 132
pm100
  • 48,078
  • 23
  • 82
  • 145
  • Agreed. Every time this happens to me I think I accidentally picked the _New JavaScript Project_ wizard. –  Mar 09 '22 at 07:46