C# Program Structure

Program Structure, Before we examine simple building blocks of the C# programming language, let us examine a naked minimum C# program structure so that we are able to take it as a reference in upcoming chapters.

Creating Hello World Program

A C# application includes the following elements −

  • Namespace declaration
  • A magnificence
  • Class techniques
  • Class attributes
  • A Main technique
  • Statements and Expressions
  • Comments

Let us study a simple code that prints the words “Hello World” −

using System;

namespace HelloWorldApplication {
   class HelloWorld {
      static void Main(string[] args) {
         /* my first program in C# */
         Console.WriteLine("Hello World");
         Console.ReadKey();
      }
   }
}

When this code is compiled and executed, it produces the following result −

Hello World

Program Structure, Hello World

Program Structure, Let us look at the various parts of the given program −

  • The first line of this system the usage of System; – the the usage of keyword is used to consist of the System namespace in the software. A software typically has multiple the usage of statements.
  • The subsequent line has the namespace statement. A namespace is a set of classes. The HelloWorldApplication namespace carries the magnificence HelloWorld.
  • The subsequent line has a category declaration, the magnificence HelloWorld contains the statistics and approach definitions that your program uses. Classes normally include more than one methods. Methods define the conduct of the magnificence. However, the HelloWorld magnificence has only one approach Main.
  • The subsequent line defines the Main technique, which is the entry factor for all C# packages. The Main approach states what the class does while done.
  • The subsequent line // is disregarded by using the compiler and it’s far put to feature feedback inside the program.
  • The Main technique specifies its behavior with the statement Console.WriteLine(“Hello World”); WriteLine is a way of the Console elegance described in the System namespace. This assertion reasons the message “Hello, World!” to be displayed on the display.
  • The remaining line Console.ReadKey(); is for the VS.NET Users. This makes the program look forward to a key press and it prevents the display from strolling and final fast while the program is released from Visual Studio .NET.

Program Structure, It is worth to notice the following factors −

  • C# is case sensitive.
  • All statements and expression need to quit with a semicolon (;).
  • The software execution begins on the Main approach.
  • Unlike Java, software report name could be distinct from the class call.

Program Structure,Compiling and Executing the Program

If you are the use of Visual Studio.Net for compiling and executing C# applications, take the subsequent steps −

  • Start Visual Studio.
  • On the menu bar, pick out File -> New -> Project.
  • Choose Visual C# from templates, after which pick out Windows.
  • Choose Console Application.
  • Specify a call on your undertaking and click on OK button.
  • This creates a brand new challenge in Solution Explorer.
  • Write code in the Code Editor.
  • Click the Run button or press F5 key to execute the project. A Command Prompt window seems that incorporates the road Hello World.

You can collect a C# application by way of using the command-line instead of the Visual Studio IDE −

  • Open a text editor and upload the above-cited code.
  • Save the record as helloworld.Cs
  • Open the command set off tool and visit the listing wherein you stored the record.
  • Type csc helloworld.Cs and press input to bring together your code.
  • If there aren’t any errors for your code, the command activate takes you to the next line and generates helloworld.Exe executable record.
  • Type helloworld to execute your software.
  • You can see the output Hello World printed on the screen.