What are the main parts of a C# program?

CsharpProgrammingServer Side Programming

The main parts of a C# program includes −

  • Namespace declaration
  • A class
  • Class methods
  • Class attributes
  • A Main method
  • Statements and Expressions
  • Comments

The following is an example showing how to create a C# program −

Example

 Live Demo

using System;
namespace Demo {
   class Program {

      static void Main(string[] args) {
         Console.WriteLine("Our first program in C#!");
         Console.ReadKey();
      }
   }
}

Output

Our first program in C#!

Here are the parts of the C# program we saw above −

  • using System; - the using keyword is used to include the System namespace in the program. A program generally has multiple using statements.

  • Namespace declaration. A namespace is a collection of classes. The Demo namespace contains the class Program.

  • The next line has a class declaration, the class Program contains the data and method definitions that your program uses. Classes generally contain multiple methods. Methods define the behavior of the class. However, the Program class has only one method Main.

  • The next line defines the Main method, which is the entry point for all C# programs. The Main method states what the class does when executed.

  • The Main method specifies its behavior with the statement Console.WriteLine("Our first program in C#!");

  • WriteLine is a method of the Console class defined in the System namespace. This statement causes the message “Our first program in C#!” to be displayed on the screen.

raja
Published on 09-Aug-2018 12:26:17
Advertisements