find and solve ||
Please wait.....

Welcome back.

Before Read Terms of use

C# Program Structure And Basic Syntax

C# - Program Structure

Before we study the basic building blocks of the C # program of language, let's look at a minimalist C # program structure so that we can refer to it in future chapters.

Creating Display First  "Hello World "Program
A part of program consists given below
Namespace declaration
A class
Class methods
Class attributes
A Main method
Statements and Expressions
Let us look at a simple code given below which is  printed the words "Hello World" −

using System;
namespace HelloWordDemo
    class HelloWorldClass
        static void Main(string[] args)
            /* my first program in C# */
            Console.WriteLine("Hello World");
Hello World

Let's look at the different parts of the given program -

using System

using keyword is used to include the System namespace in the program and program generally has multiple using statements also.


A namespace is a collection of classes.The "HelloWord" namespace contains the class HelloWorldClass.

class declaration

The class HelloWorldClass contains the method  and  data which is  definitions that your program uses.Classes are generally  contain multiple methods.Methods define the behavior of the class.Whatever, the HelloWorldClass class has only one method Main.

Main method

 The Main Method entry point for all C# programs.The Main method states what the class does when executed.
This is ignor by the compiler and also it is put to add comments in the program.

Console.WriteLine("Hello World");

In the  statement, Main method specifies its behavior .


WriteLine  is a Method and the Console  class defined under the System namespace.This Console.WriteLine statement  display 
on the screen.

The last line Is Console.ReadKey(); 

This Console.ReadKey() method  makes the program wait for a key press and it prevents the screen from running and closing quickly when the program is launched from Visual Studio .NET.

Some important points in C# given below

C# is case sensitive.
You must end with a a semicolon (;) in all statements and expression 
The program execution starts at the Main method.
Program file name could be different unlike java in the class name.

Compiling and Executing the Program

If you are using compiling and executing C# programs,Some takes given steps −
Start Visual Studio.
Go to menu bar, choose File ->> New ->> Project.
Choose Visual C# from templates, and then choose Windows.
Choose Console Application.
Specify a name for your project and click OK button.
This creates a new project in Solution Explorer.
Write code in the Code Editor.
Click the Run button or you can also  press F5 key to execute the project. Then you can see Hello Word on your windows screen.

You can compile the C # program using the command-line instead of the Visual Studio IDE -

Open a text editor and add the above-mentioned code.
Save the file as helloworld.cs
Open the command prompt tool and go to the directory where you saved the file.
Compile your code to type csc helloworld.cs and press enter.
The command prompt takes you to the next line and generates helloworld.exe executable file when there are no errors in your code.
Type helloworld to execute your program.
You can see the output Hello World printed on the screen.

Sundar  Neupane

Sundar Neupane

I like working on projects with a team that cares about creating beautiful and usable interfaces.


Report Response