By Oct 13, 2018

Description:

Here, we explain how to create a console application in C# with Visual Studio and we give simple examples of programs.

Preferencesoft

First program in C#

Here, we explain how to create a console application in C# with Visual Studio and we give simple examples of programs.

Creating a Console Application

Let's start by creating a console application with Visual Studio.

  • In the menu bar, select File > New > Project
  • In the New Project dialog box, select the C# template,
  • and then choose Application Console (.NET Framework) in the Name box, give the project the name Hello.

When you create a program in console mode with Visual Studio, multiple files and directories are automatically generated. Files containing C# code have the extension .cs. We will open the file named Program.cs in which we will program, and which contains the main program.

But this file is not empty and already contains some instructions in the program block between the two Braces.

Before we start, let's explain how to run a program and debug it. You must remember two key combination to work in Visual Studio:

Ctrl F5 (Start Without Debugging) to run the program

F5 (Start Debugging) to debug the program

If we run the program without modification, it will not really accomplish anything because there is no instruction in the Main method.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Hello
{
    class Program
    {
        static void Main(string[] args)
        {
        }
    }
}

In this code, using is a directive that allows you to use the types of a namespace without having to mention the full name.

This allows in this program to write

Console.WriteLine("Hello")

Instead of

System.Console.WriteLine("Hello")

namespace Hello is used to declare our own namespace.

We place the instructions of our program in the Main method of the System class.

My First Program

To discover a new programming language, a tradition invites you to start by writing a program that displays Hello, World! I do not know the exact genesis of this practice, but it has the advantage of being simple and not to involve many concepts that could confuse beginners. It also shows how a computer works: it reacts only according to programs and it displays hello because it's programmed for but not because it thinks.

We will add instructions to the previously created program. So, let's insert the following instructions:

            Console.WriteLine("Hello, World!");
            Console.WriteLine("Press Enter to exit");
            Console.ReadLine();

We obtain the following program:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Hello
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello, World!");
            Console.WriteLine("Press Enter to exit");
            Console.ReadLine();
        }
    }
}

The second line may be too much in some cases, if Visual Studio is in Debug mode.

We will see how to generate an executable program independent of Visual Studio. In the menu bar, click Build and select Build Hello. Then open the project directory C:\Users\user\source\repos\Hello\Hello\bin\Debug or C:\Users\user\Documents\Visual Studio 2017\Projects\Hello\Hello\bin\Debug, double click hello.exe. Your program runs.


CSharp

Categories

Share

Follow


KodFor Privacy Policy