How to Build, Compile and Run your first Program in C# ?
In this tutorial we will learn some basic things that you should know while programming.
Building the first console application
A console application is an application that can be run in the command prompt in Windows. For any beginner on .Net, building a console application is ideally the first step to begin with.
In our example, we are going to use Visual Studio to create a console type project. Next, we are going to use the console application to display a message "Hello World". We will then see how to build and run the console application.
Let's follow the below mentioned steps to get this example in place.
Step 1) Open Visual Studio 2017,
Click on : File Menu >> New >> New Project
Step 2) Select Visual C# >> Console App (.NET Framework)
Then, Name you first Console App,
Then, Change the location where you want to save your program (recommended that you change the location and create a desire folder for all your future program)
Important, Choose .NET Framework 4.7.1 (Because its the latest and easily debugable)
Step 3) Lets see quickly and know what all the thing is. Click on the image below to see it properly/enlarge
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HelloWorld
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World !");
//Console.ReadLine();
}
}
}
Now, What is Console.Write Line() , It is the function in C# which is used to print something on screen or displaying something in the string.
Press F5 to run the program. Yeah! the program will run and immediately closes and shows error
"The program '[10368] HelloWorld.exe' has exited with code 0 (0x0)."
Reason: The Program runs successfully and after that it has nothing to wait for execution.
Hence, we use Console.ReadLine(); It is the function in C# that takes input from the console. The program will wait until user press they key.
Similary as we used getch() function in C.
Now, Remove "//" before Console.ReadLine(); and Run the Program.
Hint : "//" is used for the single line comments, that will not be compiled and ignored by the compiler.
Output :
This is your first output, proceed to next tutorial !
Comments
Post a Comment