C# Program Structure


Basic Structure of a C# Program

  1. Namespace Declaration

    • Namespaces are used to organize code and prevent naming conflicts.
  2. Class Declaration

    • All C# programs are built around classes. A class is a blueprint for creating objects.
  3. Main Method

    • The entry point of a C# application. The execution of the program starts from the Main method.
  4. Methods

    • Functions defined within a class that perform specific tasks.
  5. Variables

    • Used to store data. Variables must be declared with a specific type.

Example of a C# Program

Here’s a simple example of a C# program that demonstrates the above structure. This program takes user input, processes it, and displays a message.

csharp
1using System; // Importing the System namespace 2 3namespace HelloWorldApp // Declaring a namespace 4{ 5 class Program // Declaring a class named Program 6 { 7 // The Main method - the entry point of the program 8 static void Main(string[] args) 9 { 10 // Declare a variable to store user input 11 string userName; 12 13 // Prompt the user for input 14 Console.Write("Enter your name: "); 15 16 // Read user input from the console 17 userName = Console.ReadLine(); 18 19 // Call a method to display a greeting 20 GreetUser (userName); 21 } 22 23 // A method to greet the user 24 static void GreetUser (string name) 25 { 26 // Display a greeting message 27 Console.WriteLine($"Hello, {name}! Welcome to the C# program."); 28 } 29 } 30}

Explanation of Each Part

  1. Using Directive

    csharp
    1using System;
    • This line imports the System namespace, which contains fundamental classes and base classes that define commonly-used types, such as Console.
  2. Namespace Declaration

    csharp
    1namespace HelloWorldApp
    • This declares a namespace called HelloWorldApp. Namespaces help organize code and prevent naming conflicts between classes.
  3. Class Declaration

    csharp
    1class Program
    • This defines a class named Program. In C#, all executable code must reside within a class or struct.
  4. Main Method

    csharp
    1static void Main(string[] args)
    • The Main method is the entry point of the program. It is marked as static, meaning it can be called without creating an instance of the class.
    • The string[] args parameter is an array of strings that can hold command-line arguments (not used in this example).
  5. Variable Declaration

    csharp
    1string userName;
    • This line declares a variable userName of type string to store the user's name.
  6. Input and Output

    csharp
    1Console.Write("Enter your name: "); 2userName = Console.ReadLine();
    • Console.Write displays a prompt for the user to enter their name.
    • Console.ReadLine() reads the input from the console and stores it in the userName variable.
  7. Method Call

    csharp
    1GreetUser (userName);
    • This line calls the GreetUser  method, passing userName as an argument.
  8. Method Definition

    csharp
    1static void GreetUser (string name) 2{ 3 Console.WriteLine($"Hello, {name}! Welcome to the C# program."); 4}
    • This defines a method called GreetUser  that takes a string parameter name.
    • It uses string interpolation (the $ symbol) to create a greeting message that includes the user's name and displays it using Console.WriteLine().

Talk to us?

Post your blog