Basic C# Console Application template

Marc Menzel 121 Reputation points
2023-01-28T00:28:52.43+00:00

I am new to C#. I am confused on how to initiate a C# console application. I tried some examples except none of them seem to work. The format of a C# Console Application has changed a lot recently, so I don't know how to start. Could someone help get me started?

Developer technologies C#
0 comments No comments
{count} votes

Accepted answer
  1. Reza Aghaei 4,986 Reputation points MVP Volunteer Moderator
    2023-01-28T01:09:06.2533333+00:00

    No matter what version of framework you are using, the following will always work:

    using System;
    
    namespace MyApp 
    {
        internal class Program
        {
            static void Main(string[] args)
            {
                Console.WriteLine("Hello World!");
            }
        }
    }
    

    However, with newer versions of framework and SDK, the following works for .NET 6+:

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

    Above is a complete console app, when the top-level statements are enabled in project:

    <Project Sdk="Microsoft.NET.Sdk">
    
      <PropertyGroup>
        <OutputType>Exe</OutputType>
        <TargetFramework>net6.0</TargetFramework>
        <ImplicitUsings>enable</ImplicitUsings>
        <Nullable>enable</Nullable>
      </PropertyGroup>
    
    </Project>
    
    
    

    Learn more about new project template

    To learn more about the new template, take a look at the following article

    Which explains new features like:

    • Implicit using directive
    • Global using directives

    Choose between old and new project template

    When you create a new .NET (not .NET Framework) console application, you can choose between the old style or new style:

    User's image

    Tutorial - Create a .NET console application using Visual Studio

    You can learn more about console applications in the following articles:

    1 person found this answer helpful.
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Karen Payne MVP 35,586 Reputation points Volunteer Moderator
    2023-01-28T02:53:01.03+00:00

    @Reza Aghaei has provided you with the basics, if you want ready to run code samples check out my article Console Application in C#. And there is a GitHub repository with about 84 projects which include menus and other cool stuff.

    aaaaaa

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.