Basic Structure of a C# Program
Namespace Declaration
- Namespaces are used to organize code and prevent naming conflicts.
Class Declaration
- All C# programs are built around classes. A class is a blueprint for creating objects.
Main Method
- The entry point of a C# application. The execution of the program starts from the
Main
method.
- The entry point of a C# application. The execution of the program starts from the
Methods
- Functions defined within a class that perform specific tasks.
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.
csharp1using 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
Using Directive
csharp1using System;
- This line imports the
System
namespace, which contains fundamental classes and base classes that define commonly-used types, such asConsole
.
- This line imports the
Namespace Declaration
csharp1namespace HelloWorldApp
- This declares a namespace called
HelloWorldApp
. Namespaces help organize code and prevent naming conflicts between classes.
- This declares a namespace called
Class Declaration
csharp1class Program
- This defines a class named
Program
. In C#, all executable code must reside within a class or struct.
- This defines a class named
Main Method
csharp1static void Main(string[] args)
- The
Main
method is the entry point of the program. It is marked asstatic
, 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).
- The
Variable Declaration
csharp1string userName;
- This line declares a variable
userName
of typestring
to store the user's name.
- This line declares a variable
Input and Output
csharp1Console.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 theuserName
variable.
Method Call
csharp1GreetUser (userName);
- This line calls the
GreetUser
method, passinguserName
as an argument.
- This line calls the
Method Definition
csharp1static 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 parametername
. - It uses string interpolation (the
$
symbol) to create a greeting message that includes the user's name and displays it usingConsole.WriteLine()
.
- This defines a method called