Introduction

As we know .NET 6 is the latest version of .NET and is generally available with lots of improvement in performance, security, code quality, developer experience and provides long-term support. It is recommended for beginners or newbies who want to start their .NET learning journey using the latest framework and latest Visual Studio IDE.

This article describes how to get started with .NET 6 and what is new in .NET 6 with comparison to .NET5. We will create a console application in .NET6 and .NET 5 and compare the differences between them. Additionally, the article will show how to add more classes in .NET 6 console application and call it from the Program.cs.

Let’s move on.

Create Console App in .NET6

Step 1- Open Visual Studio 2022 and click Create a new project.

Step 2 – Select Console App and click Next.

Step 3 – Give the project name and location of the project.

Step 4 – Select framework: .NET 6.0 (Long-term support).

This creates the console app which looks like below.

Default Program.cs file is listed below.

// See https://aka.ms/new-console-template for more information
Console.WriteLine("Hello, World!");

Now, let’s compile and run the program. Click on Strat without debugging option or press Ctrl+F5 as illustrated below.

You can see the FirstConsoleApp.exe is created in the folder location of the project under the directory: “E:\SampleProject\FirstConsoleApp\bin\Debug\net6.0” as shown below.

We can run this .exe file which displays the “Hello, World!” message.

Create Project in .NET 5

Let’s create a console application in Visual Studio using .NET 5. Follow all steps as per previous, only choose framework .NET 5 in Step 4.

Below is Program.cs of console app using .NET 5.

Comparing .NET6 and .NET 5

We can see here, Program.cs of .NET 5 contains:

  • Using system
  • Namespace
    class keyword
  • Main method

If we compare both the Program.cs file from the above two console apps, these are not available in .NET 6. so, this is the difference between .NET5 and .NET6.

Extending .NET 6 Console Application

Add New Class

Right-click on the project—Go to Add->Class and click add.

We have added a class with the name: Class1.

Now, will create a public void method named Sum: in this Class1.

public void Sum()
        {
            int a = 5;            
            int b = 6;
            int Sum = a + b;
            Console.WriteLine("Sum : {0}", Sum);
        }

Complete code of Class1.cs

namespace FirstConsoleApp
{
    internal class Class1
    {
        public void Sum()
        {
            int a = 5;            
            int b = 6;            
            int Sum = a + b;
            Console.WriteLine("Sum : {0}", Sum);
        }        
    }
}

You might have noticed that this class contains:

  • the namespace
  • internal Class
  • using statements (by default)

Whereas Program.cs doesn’t contain those and you can write logic directly.

Call Sum() Method

In Program.cs class we will call the sum method of the Class1.cs. Furthermore, to call a method from class, here we need to add namespace: using FirstConsoleApp;

using FirstConsoleApp; //need to call method from Class1
Class1 class1 = new Class1();
class1.Sum();

Now, if we run the app then we will get Sum:11 in the console.

Complete code of Prgram.cs

// See https://aka.ms/new-console-template for more information
using FirstConsoleApp;
Console.WriteLine("Hello, World!");
Class1 class1 = new Class1();
class1.Sum();

When we run the console application, the output looks like below.

Conclusion

Hence, in this article, we created our first Console application in .NET 6 and also created a console app in .NET 5 and compared the differences between them. Additionally, we added the new class in .NET 6 console application and learned to call the method from class in Program.cs file. I hope it helps you to understand the differences between .NET 5 and .NET 6 and get start your journey in the latest .NET framework.

17 thoughts on “Getting Started With .NET 6.0 Console Application”
  1. I was recommended this web site by my cousin. I’m not sure whether this post is written by him as nobody else know such
    detailed about my trouble. You’re amazing! Thanks!

  2. I always spent my half an hour to read this website’s
    articles all the time along with a cup of coffee.

  3. I’m now not positive where you are getting your info,
    but good topic. I needs to spend a while studying much more or figuring
    out more. Thank you for great info I used to be on the lookout for this information for my
    mission.

  4. First off I want to say great blog! I hadd a quick questoon in whikch I’d like to assk if you don’t mind.
    I was interested to know how you centerr yourself and clear your
    mind prior to writing. I’ve had a tough time clearing
    mmy mind in getting my ideas out. I do take pleasure in writing however it just seems like the first 10 to 15 minutws are
    wasted simply just trying to figure out how tto begin. Any recommendations or hints?
    Thanks!

  5. I do not even know how I ended up here, but I thought this post was good.

    I don’t know who you are but certainly you are going to a famous blogger if you aren’t already ;
    ) Cheers!

  6. Usually I don’t read article on blogs, but I would like to say that
    this write-up very forced me to try and do it! Your writing style has been amazed me.
    Thanks, quite nice post.

  7. I always used to study post in news papers but now as I am a user of net thus from now I am using net for articles or
    reviews, thanks to web.

  8. Good web site you have got here.. It’s difficult to find quality writing like yours nowadays.
    I honestly appreciate individuals like you! Take care!!

  9. When I initially commented I clicked the “Notify me when new comments are added” checkbox
    and now each time a comment is added I get three e-mails with
    the same comment. Is there any way you can remove people from that service?
    Thanks!

  10. My programmer іs ttrying to persuade me to move to .net
    from PHP. I hɑve aⅼwɑys disliked tһe idea Ƅecause of the expenses.
    Вut he’s tryiong none tһe lеss. Ι’ve beеn using Movable-type
    on а variety ߋf websites for about a үear and am concerned about switchhing tto another platform.
    I һave heɑгd very good thingѕ about blogengine.net. Ιѕ thеre а waay I can transfer aⅼl
    my wordpress posts іnto it? Any help ѡould be greatⅼy
    appreciated!

  11. Nice blog here! Also your website loads up very fast!
    What web host are you using? Can I get your affiliate link to your
    host? I wish my website loaded up as fast as yours lol

Leave a Reply

Your email address will not be published. Required fields are marked *