Home > C# > Basic Structure of C # program

C# programming

To start learning C#, we must know about the basic structure of C# program. i.e. how to write a program in c#. What are the rules to follow in c# program structure?
A C# program consists of following:
using statements: – Using statements import the types in the namespace, so you can use a partial-qualified type name, instead of having to use the fully-qualified type name. It does not include any file content or any reference. we can add more than 1 using statements in our program.

namespace declaration: – A namespace is a collection of classes or types.

class declaration: – It is a class just like we have in c++.

class attributes and methods: – Class attributes are the variables declared in the class body. They can be public, private or protected as per requirements. Methods are the member functions we have in our class. Mostly they are public. But it is not necessary to keep them public. More examples of attributes and methods will be discussed later. These are similar to we have in C++.

Note: –Here are the things we should keep in mind while writing a C# program.

C# is a case sensitive language. i.e., double x and double X are two different variables.

Every statement in the c# program ends with a semicolon (;) just like in C++.

The main method Unlike Java, it is not necessary that file name and class name should be same.

What is the basic structure of C# program?

A C# program structure is as follows:

using System;
namespace programs
{
    class Program  // class initialization
    {
        static void Main(string[] args)  /* main method 
        it defines entry point for a C# program. 
        It is a point from where every program in 
        C# starts its execution. */
        {
            // Main Function Body
        }
    }
}

First Program

Let us write our first program in C#.

using System;

namespace first_Program
{
     class Program
     {
          static void Main(string[] args)
          {
               Console.WriteLine("This is our first program written in C#.");
               Console.ReadKey();
          }
     }
}

Output: –

basic structure of c# program - C# program structure

The System namespace contains the Console type because the fully-qualified name is System.Console and the Console class defines the WriteLine method. We use WriteLine to write a string to the console.

ReadKey is used to hold the screen as it waits for a key to be pressed by the user. In this way, we can prevent a program from running and closing quickly.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.