C# Console Application: How to write First C# Hello World Program

C# Console applications

C# console Application:- Most PC users are used to seeing the programs appear as windows on the screen. However, this requires the program to communicate with the window manager of the operating system and uses special options and functions of the operating system. Applications that do not require window-based, graphical user interface (GUI) manage, but can do without it and instead use the C# Console to exchange data with the user.

What is the console?

The console is a special environment that makes the application believe that long live in the good old days, when there were no “window” systems and only one application was running at a time could. This application could then have unlimited access to all of the computer’s resources for example the keyboard as the most important input device, or the screen as the most important Output device. The screen was usually switched to text mode, so it was not made up of rows of pixels built up, but from lines of text. On Windows, the console is called MS-DOS Prompt, or just Command Prompt and can be accessed via Start / All Programs or Start / All Programs / Accessories, depending on the operating system will.



Creating C# Hello World Application:

Step 1: Create a project

  • In the File menu, select the New / Project command.
  • In the New Project dialog box, select Visual C # / Windows as the project type and then select the template C# Console application.
  • Enter HelloWorld as the name for the project, choose a location that suits you and deselect the option Create Solution Directory. Visual C # creates a separate subdirectory for the project under the selected storage location, which bears the name of the project. If you also have a project folder created, the project folder is saved as a subdirectory under the specified storage location and the project directory as a subdirectory of the project folder furnished. If you clear the option for the solution directory, the project directory becomes created directly under the selected storage location.
  • Confirm the dialog box with OK.

c# console application

The Visual C # development environment now creates the project, including a source text file named Program.cs. The source text file is automatically loaded into the integrated editor for editing. in You can use the Solution Explorer (accessed via View / Solution Explorer or (Ctrl) (W), (S)) Get an overview of the source files and other parts of the project.



Step 2: Edit the source code

The basic C# Console structure created by Visual C# consists of a Program class with a Main () Method. For smaller applications, you only need the instructions to be executed in the Main () method to be entered. For larger applications, you’ll add more methods of your own Define classes, distribute the program source code to several source files if necessary. We are content here with outputting a short message to the C# Console.

 Use the Console.WriteLine () method to say “Hello world!”:


Step 3: compile

To compile the project, you only need to call its Build command.

Compile the project with the menu command Create/Create HelloWorld. If the compiler detects that the source text is syntactically incorrect or that there are suspect areas contains the error list window with error messages and warnings. Could Project can be created without any problems, the message Create successful appears in the status bar.

Step 4: execute

After a successful compilation, you can run and test the application. To do this, you need the Don’t even leave the Visual C # environment. Call the menu command Debug / Start without debugging. The Visual C# IDE opens a C# Console window, runs the C# Console application, and routes the output of the application to the console. After the application finishes, Visual C # still holds the console window further open so that you can review the application’s output. To close the window just press any key (while the C# Console window is activated).

c# console application

Of course, the C# Console application can also be started from the operating system level. You will find the .exe file in the project subdirectory bin \ Debug (or bin \ Release), if you created the project in the release or another configuration <in my case my path is C:\Users\Fawad khan\AppData\Local\Temporary Projects\HelloWorld\bin\Debug>.

You can start the C# Console application directly from Windows Explorer by double-clicking it. In the case of our demo application HelloWorld.exe you will not see much of the program, because the C# Console window that is opened for the C# Console application closes automatically as soon as the application has ended. With HelloWorld.exe you will probably only see a brief flicker of the See C# Console application. It is better to open a console window yourself and start the C# Console application from there.


Run the C# Console Application through the command prompt:

Open a command prompt window (depending on the operating system via Start / All Programs or Start / All Programs / Accessories). Use the command cd <in my case my path is C:\Users\Fawad khan\AppData\Local\Temporary Projects\HelloWorld\bin\Debug>  to change to the directory in which the .exe file of the application is located.

To do this, simply type in the name of the .exe file at the C# Console prompt and confirm by pressing the (enter) button.

c# console application

Step by Step Programming Explanation of C# Console Application:

class:

In C #, the concept of class is practically ubiquitous: be it as an element of the program layout (see above), as a user-defined data type or as the heart of the object-oriented Programming in C#. Just be brief here noted that classes represent types that are composed of various elements (members). The most important members are fields (class-internal variables) and methods (class-internal functions). A distinction is made between static and non-static members according to their use and meaning for the class.

Static members that are defined with the static keyword can be identified directly by the name of the Class to be called: ClassName.Member.

Non-static members can only be addressed via objects. Objects are instances of a Class created using the new keyword. The new object is given when the instance is created a copy of each (non-static) field in the class. Are via an object (non-static) methods called of the class, they operate on the data of the object. Non-static members also become referred to as an instance member.


static void Main(string[] args) entry point:

Execution of a C# console application or window application begins with the Main() method, which in C# does one of the following Must have signatures:

Return value and batch files

If Main() is declared with the return value int, it must end with a return statement, which returns an integer.

This is always interesting when the application returns different values ​​depending on the course of the session which are then evaluated by DOS batch files that call the application can.

Parameters and command-line arguments

If the user calls up a program via the console (command prompt), he can add the program Transfer data directly when called. The data is simply used as arguments after the program name listed.

Prompt:> Program name 1 2

To receive the command line arguments in the application, you must use Main () with a string [] – define parameters.


directive and framework classes:

Programming with C # is hardly conceivable without the functionality of the .NET Framework classes, yes simply impossible. Whether you output data to the console, query the time, graphical user interfaces create or want to run multiple threads in parallel … for almost all important Programming tasks can be found in the .NET Framework predefined classes whose functionality you can fall back. The classes of the .NET Framework library are distributed over several DLL assemblies (System.dll, System. Data.dll, System.Windows.Forms.dll, System.XML.dll …). To use classes from these assemblies you will need to compile your C # code along with references to the assemblies. In Visual Studio, you set up references using the context menu commands of the project sub-node of the same name. If you are working directly with the csc compiler, use the compiler option / r to access the Reference DLLs. You do not have to list the DLL System.dll explicitly. You can use the classes of the .NET Framework directly in your source code, but you must note that the classes are organized in a hierarchical structure of namespaces. You must therefore either:

Prefix the full namespace path to the class name:

or at the beginning of the source text, insert a using directive that takes all the names from the Exposes namespace:

 

Recommended For You

About the Author: Fawad

My name is Shahzada Fawad and I am a Programmer. Currently, I am running my own YouTube channel "Expertstech", and managing this Website. My Hobbies are * Watching Movies * Music * Photography * Travelling * gaming and so on...

1 Comment

Leave a Reply

%d bloggers like this: