SEARCH C# PROGRAMMING CODES HERE

C# - Programming Structure

Programming Structure
Before we start programming let’s understand the concept for building blocks of code in C# programming language, First, we will understand about the program structure which is basically a collection of variables of different data types under a single unit. It is almost similar to the class because both are user-defined data types and both hold different data types, So let’s start Compiling and Executing the Program using Visual Studio C# programs, go through the following steps one by one-

1-Start Visual Studio.
2-On the menu bar, choose File -> New -> Project.
3-Choose Visual C# from templates, and then choose Windows.
4-Choose Console Application.
5-Specify a name for your project as Hello_World and click OK button.

6-Now, let’s create a simple program which prints the words “Hello World” on the user screen, On the program.cs window as shown below.


Now simply copy the yellow highlighted code as follows.

using System;
namespace Hello_World
{
 Class Program
{
 static void Main(string[] args)
 {   
 Console.WriteLine("Hello World");
 Console.ReadKey();
 }
 }
}

Now Click the Run button or press F5 key to execute the project.
When this code is compiled and executed, it produces the following result.
Hello World

Now we will go through all the concept one by one that we have implemented in the above program as shown below.

/* program structure*/

/*Namespace declaration */
using System;
/*Class Declaration*/
namespace Hello_World
{
    /* Method of class*/
   Class Program
{
        /* Main Method */
      static void Main(string[] args)
 {
         /* To Display text on the user screen */
         Console.WriteLine("Hello World");
/*prevents the screen from closing */
         Console.ReadKey();
      }
   }
}
  
Now will see one by one how the program structure work.

Namespace declaration
The first line of the program is using System; - it’s used to include the System namespace in the program. A program generally has multiple using statements according to the programming needs.

Class Declaration
The next line has the namespace Hello_World declaration which is a collection of classes. The Hello_World namespace contains the Class Program.

Class
Class is generally used in large programs and also it has limitless features.
A Class can inherit from another class. The data member of a class can be protected and also a Function member of the class can be virtual or abstract.

Class methods
In the above program of the class declaration, the Class Program contains the data and method definitions that your program uses. Classes generally contain multiple methods which define the behavior of the class. However, the Hello_World class has only one method Main.

Main method
The next line defines the Main method, which is the starting point for all C# programs. The Main method states what the class does when executed and also the execution of the program, starts at the Main method. The Main method static void Main(string[] args) specifies its behavior with the statement.


Statements and Expressions
 WriteLine is a method of the Console class defined in the System namespace. All statements and expression must end with a semicolon (;).
This statement causes the message "Hello World" to be displayed on the user screen.
Console.WriteLine("Hello World");

Comments
The next line /*...*/ is ignored by the compiler and it is put to add comments in the program.

The last line was the Console.ReadKey(); it’s used to prevents the screen from running and closing quickly when the program is launched and also It obtains the next character or function key pressed by the user.