Program Structure
A C# program is organized like a school: namespaces are the buildings, classes are the classrooms, and methods are the lessons inside. Knowing where things go helps you find them later.
In this page:
Syntax
using System;
namespace NamespaceName
{
class ClassName
{
static void Main(string[] args)
{
// statements
}
}
}
Program Structure
A C# file is organized into namespaces, classes, and methods, with Main acting as the program's entry point. Statements inside Main execute top to bottom.
Understanding this structure is essential before writing anything more complex than Hello World.
Note:
Only one
Main method is allowed to be the entry point of an executable project.Example: Program Structure
using System;
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Program started");
Greet();
Console.WriteLine("Program finished");
}
static void Greet()
{
Console.WriteLine("Hello from a helper method!");
}
}
Login to try C/C++/Java/PHP code in the editor
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the
view already swaps in the hi_ translation fields (or blanks these
out if untranslated), so this renders correctly for both languages
without a lang_code check here. #}
Common Mistakes
- Forgetting that a program needs an entry point, such as
Mainor top-level statements, in only one file. - Putting top-level statements in more than one file, which is a compile error.
- Mismatching braces in nested namespaces, classes or methods, giving errors far from the real mistake.
🔒
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: