- C# Basics
- C# Home
- C# Program Structure
- C# Basic Syntax
- C# Data Types
- C# Variables
- C# Type Conversion
- C# Constants
- C# Operators
- C# Decision Making
- C# Loops
- C# Arrays
- C# Strings
- C# Methods
- C# Nullables
- C# Structure
- C# Enum
- C# Object Oriented
- C# Classes
- C# Encapsulation
- C# Inheritance
- C# Polymorphism
- C# Operator Overloading
- C# Advance
- C# Namespaces
- C# Interfaces
- C# Preprocessors
- C# Regular Expressions
- C# Exception Handling
- C# File I/O
- C# Test
- C# Online Test
- Give Online Test
- All Test List
C# Program Structure
C# program structure comprises from these parts:
- namespace declaration
- a class
- class attributes
- class methods
- a main method
- statements and expressions
Comment may also be included, it is optional, since comments are ignored by the C# compiler.
Example
Here is a simplest C# program given, for the understanding of general structure of a C# program:
/* C# Program Structure - Example Program */ using System; namespace HelloWorldApp { class HelloWorldClass { static void Main(string[] args) { /* this is my first program in C# language */ Console.WriteLine("Hello World"); Console.ReadKey(); } } }
When the above code is compiled and executed, it will produce the following output:
Hello World
Here is the explanation of the above C# program:
- /* C# Program Structure - Example Program */ - this line is ignored by the C# compiler, this is the comment in the program
- using System; - the using keyword is simply used to include the System namespace in C# program.
- namespace HelloWorldApp - this is the namespace declaration. A namespace is simply a collection of classes. The HelloWorldApp namespace contains the class named HelloWorldClass
- class HelloWorldClass - here the class HelloWorldClass contains the data and the method definitions that your C# program uses
- static void Main(string[] args) - this line defined the Main method, which is the entry point for all the c# programs
- /* this is my first program in C# language */ - this line is also ignored by the C# compiler, this is also a comment in the program
- Console.WriteLine("Hello World"); - this is the behaviour of the Main method. Here the WriteLine() is the method of the Console class which is defined in the System namespace. This statement causes the message "Hello World" to be displayed on the output screen
- Console.ReadKey(); - this is for the VS.NET users. This statements makes the C# program wait for a key press and it prevents the sudden closing of the output screen when the program is launched from the Visual Studio .NET
Here are the steps to compile and execute the above C# program using Visual Studio .NET (VS.NET):
- open visual studio
- on the menu bar
- choose FILE -> NEW -> PROJECT
- choose visual C# from templates
- choose windows
- choose console application
- specify a name for your project
- click OK
After processing the above given steps, your new project in the Solution Explorer is created. Now just write the above code in the Code Editor. After writing the code, to run your C# program, just press F5 key or click on the RUN button to execute the project. After performing this, a command prompt window will appears, containing the line, Hello World.
« Previous Tutorial Next Tutorial »