Hello Wolrd, first C# program
In this tutorial, you'll learn how to create a simple C# console application and understand its basic elements.
Introduction to C# Applications
C# is a flexible language used for a variety of applications, including window-based, web-based, and console applications. We'll start by focusing on building a console application using C#.
Getting Started
First, make sure that you have Visual Studio (2017/2019/2022 or a newer version) installed on your PC.
Creating a Console Application in Visual Studio
Here's how you can start creating a console application with Visual Studio: Go to the top menu and select File -> New Project..., as shown in the following image.
Step-1
To start, you'll need to set up a new project in Visual Studio. First, open Visual Studio 2022, which is the latest version. Then, choose the "Create a New Project" option, as shown in the image below.
Step-2
Next, you need to choose the type of project you want to create. For this, type "Console" in the search bar. This will show you different console application options that use languages like C#, VB, and F#, and work with either the .NET Framework or Core Framework. In our case, we'll select the Console App (.NET Framework) using C#, and then click the 'Next' button, as you can see in the image below.
Step-3
Moving forward, the next step is to set up your new project. During this phase, you need to provide a name for both the project and the solution. Remember, these names don't have to be the same. You also need to choose where to save your project. Moreover, it's important to select which version of the .NET Framework you want to use. The most recent version is 4.8, and for this example, we'll use .NET Framework 4.8. Then, as shown in the image below, click on the 'Create' button to continue with setting up your project.
Step-4
Once you click the 'Create' button, Visual Studio will set up your Console Application and you'll see its structure appear.
Step-5
Visual Studio will create a project called "HelloWorldConsoleApp1" that includes all the necessary files for running a Console application. When you start a new console application in Visual Studio, it automatically creates a main program file named Program.cs. This file, Program.cs, holds the important code that your console application needs. When you open the Program.cs class file, you'll see the code snippet inside it.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HelloWorldConsoleApp1
{
internal class Program
{
static void Main(string[] args)
{
}
}
}
Step-6
Let us proceed with writing the code responsible for displaying the message:
"Hello world! This is my first application"
Please visit my website https://www.dotnetustad.com
To do this, you need to change the Main method in the Program class, as shown in the code snippet below. We'll explain this code in detail in a future article. For now, you can just copy and paste the code.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HelloWorldConsoleApp1
{
internal class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello world! This is my first application");
Console.WriteLine("Please visit my website https://www.dotnetustad.com");
Console.ReadKey();
}
}
}
Step-7
In this step, we'll execute the program. To run the program in Visual Studio 2022, press the "Start" button as indicated below:
After running the program mentioned above, the following output should appear in the console window: