25

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?

0

3 Answers 3

22

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

Sign up to request clarification or add additional context in comments.

4 Comments

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 the second part of the answer addresses your automatic concerns and as pointed out, is actually Microsoft’s recommendation too
@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?
@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.
14

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.


1 Comment

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?
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

1 Comment

Agreed. Every time this happens to me I think I accidentally picked the New JavaScript Project wizard.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.