• Skip to main content
  • Skip to primary sidebar
  • Skip to secondary sidebar
  • Skip to footer

Computer Notes

Library
    • Computer Fundamental
    • Computer Memory
    • DBMS Tutorial
    • Operating System
    • Computer Networking
    • C Programming
    • C++ Programming
    • Java Programming
    • C# Programming
    • SQL Tutorial
    • Management Tutorial
    • Computer Graphics
    • Compiler Design
    • Style Sheet
    • JavaScript Tutorial
    • Html Tutorial
    • Wordpress Tutorial
    • Python Tutorial
    • PHP Tutorial
    • JSP Tutorial
    • AngularJS Tutorial
    • Data Structures
    • E Commerce Tutorial
    • Visual Basic
    • Structs2 Tutorial
    • Digital Electronics
    • Internet Terms
    • Servlet Tutorial
    • Software Engineering
    • Interviews Questions
    • Basic Terms
    • Troubleshooting
Menu

Header Right

Home » C# » Introduction » A Simple C# Program
Next →
← Prev

A Simple C# Program

By Dinesh Thakur

Let’s begin in the traditional way, by looking at the code of a Hello World program.

1. using System;

2. public class HelloWorld

3. {

4. public static void Main()

5. {

6. II This is a single line comment

7. /* This is a

8. multiple

9. line comment */

10. Console.WriteLine(“Hello World! “);

11. }

12. }

• The first thing to note about C# is that it is case-sensitive. You will therefore get compiler errors if, for instance, you write ‘console’ rather than ‘Console’.

• The second thing to note is that every statement finishes with a semicolon (;) or else takes a code block within curly braces.

We’ll be covering the following topics in this tutorial:

  • Explanation of Program
  • Instruction for Saving the Program
  • Writing program in Computer
  • Using Text Editor:
  • Using Visual Studio.NET
  • Compilation and Execution
  • For Visual Studio Users:
  • Here is execution of the application. :
  • Different Ways of C# Program

Explanation of Program

Line 1: using System;

We are using the System namespace. The point of this declaration is mostly to save ourselves time typing. Because the ‘Console’ object used in line 10 of the code actually belongs to the ‘System’ namespace, its fully qualified name is ‘System.Console’. However, because in line 1 we declare that the code is using the System namespace, we can then leave off the ‘System.’ part of its name within the code.

Line 2: public class HelloWorld

As C# is an object-oriented language, C# programs must be placed in classes. This line declares the class to be named ‘HelloWorld’.

 

Line 4: public static void Main()

 

When compiled and run, the program above will automatically run the ‘Main’ method declared and begun in this line. Note again C#’s case-sensitivity – the method is ‘Main’ rather than ‘main’.

Line 3, 11 and 5, 12:

These lines uses the ‘{‘for starting braces and ‘}’ for closing braces of block.

 

Lines 6-9: Comments

(‘//’ uses for single line and ‘/*…………. */’ uses for multiple line comments)

These lines of the program are ignored by the compiler, being comments entered by the programmer for his own benefit.

 

Line 6 shows a single line comment, in which everything on the line after the two forward slashes is ignored by the compiler.

 

Lines 7-9 demonstrate a multi-line comment, in which everything between the opening /*and closing */ is ignored, even when it spans multiple lines.

 

Line 10: Console.WriteLine (“Hello World”);

The statement on this line calls the ‘WriteLine’ method of the Console class in the System names pace. It should be obvious how this works in the given example – it just prints out the given string to the ‘Console’ (on PC machines this will be a DOS prompt).

Instruction for Saving the Program

In order to run the program, it must first be saved in a file. Unlike in Java, the name of the class and the name of the file in which it is saved do not need to match up, although it does make things easier if you use this convention. In addition, you are free to choose any extension for the file, but it is usual to use the extension ‘.cs’.

Writing program in Computer

There are two ways of program writing in computer

• Using Text Editor

• Using Visual Studio.NET

Using Text Editor:

We are giving the steps for writing the program in text editor, notepad.

 

1. Start Notepad

2. In the empty file, type the following

                          Using Text Editor

3. To save the file, on the main menu, click File -> Save

4. Select and display the C: (or the D 🙂 drive in the Save in combo/box

5. Click the Create New Folder button

6. Type Exercise! and press Enter twice or display the new folder in the Save In combo box

7. Save the file as exercise.cs using double quotes.

                          . Save the file as exercise.cs using double quotes

Using Visual Studio.NET

To create a console application using Visual Studio .NET follow the steps which are given below:

 

  1. Open Visual Studio .NET.

                          Open Visual Studio .NET

2. Create a new console application project by selecting the

File -> New -> Project ….. Menu item:

                     Menu item

3. Select the Visual C# Projects folder from Project types: Pane of the window that appears, and Console Application project type in the Template: pane.

                            Select the Visual C# Projects folder from Project types

4. Click the OK button. Once the project is initialized, add the code of program to file displayed in the main window:

                             Code main window

5. Save the application file by selecting the File I Save Class1.cs or press Ctrl +S.

                             Save the application file by selecting the File

Compilation and Execution

 

The compiler that ships with the C# version we will use, that is, the compiler of the .NET

Frameworks a program called csc. Like most other programs, it has the extension .exe. This csc name is not standard. This means that another C# compiler may have another name; csc.exe is just the name of the compiler we will use.

To use csc.exe, you should first find it in your computer. By default, it is installed in a

Folder like C:\ WINDOWS \ Microsoft.NET \ Framework. To use this compiler in all of your programs, you should include it in your system path.

After saving the file as ‘exercise.cs’.

To compile the program – open the Command Prompt and change to the folder in which

You created the C# file:

C:\ > cd Exercise l

C:\ Exercise l> csc exercise.cs

You Will see the following output on your screen in the dos box.

Microsoft (R) Visual C# Compiler Version 7.00.9254 [CLR version vl.0.29l4]

copyright (C) Microsoft Corp 2000-2001. All rights reserved.

Its means your program is successfully compiled. Other wise it will show the error, to remove go back to the program.

This command would generate the executable exercise.exe, which could be run in the usual way, by entering its name:

C:\Exercise1> exercise

Fairly obviously, this program would produce the output:

Hello World!

For Visual Studio Users:

  1. Compile the application by selecting Build | Build Solution or press Ctrl + Shift + B.

                                Compile the application by selecting Build

2.    To execute the application select Debug I Start without Debugging or press Ctrl + F5.

                                 To execute the application select Debug

Here is execution of the application. :

                                 execution of the application.

Different Ways of C# Program

The following examples show different ways of writing the C# Hello World Program.

Normal Program

public class Helloworld

{

public static void Main()

{

System.console.writeLine(“Hello world!”);

}

}

Output of Example

Hello World!

To avoid fully qualifying classes throughout a program, we can use the ‘using’ directive

Using system;

Public class Helloworld

{

public static void Main()

{

console.writeLine(“Hello world!”);

}

}

 

Output of Example

 

Hello World!

 

To return a return code, change the prototype of Main function

using system;

public class Helloworld

{

Console.writeLine(“Hello world!”) ;

return 0;

}

}

 

Output of Example

Hello World!

All of the above three examples will generate the same output. In first example we have used ‘System’ namespace with in the program. In the second example we have used ‘System’ namespace as directive with ‘using’ keyword. And in the third example we have used ‘Main’ function with return value.

You’ll also like:

  1. Write A C++ Program To A Simple Program That Demonstrates Void ().
  2. Write A C++ Program To Simple #if Example.
  3. Write a C++ Program to simple #if/#else example.
  4. C Program for Simple Calculator
  5. A Simple Program That is Written in C++ Language
Next →
← Prev
Like/Subscribe us for latest updates     

About Dinesh Thakur
Dinesh ThakurDinesh Thakur holds an B.C.A, MCDBA, MCSD certifications. Dinesh authors the hugely popular Computer Notes blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps.

Dinesh Thakur is a Freelance Writer who helps different clients from all over the globe. Dinesh has written over 500+ blogs, 30+ eBooks, and 10000+ Posts for all types of clients.


For any type of query or something that you think is missing, please feel free to Contact us.


Primary Sidebar

C# Tutorials

C# Tutorials

  • C# - .NET Languages Types
  • C# - Dot Net
  • C# - Dot Net Framework
  • C# - Inheritance Types
  • C# - Features
  • C# - CTS
  • C# - CLS
  • C# - CLR
  • C# - Console
  • C# - MSIL
  • C# - Base Class Library
  • C# - Web Forms Creation
  • C# - C# Vs C++
  • C# - Statements Types
  • C# - JIT
  • C# - CLI
  • C# - Controls Types
  • C# - String Types
  • C# - Execution Model

Other Links

  • C# - PDF Version

Footer

Basic Course

  • Computer Fundamental
  • Computer Networking
  • Operating System
  • Database System
  • Computer Graphics
  • Management System
  • Software Engineering
  • Digital Electronics
  • Electronic Commerce
  • Compiler Design
  • Troubleshooting

Programming

  • Java Programming
  • Structured Query (SQL)
  • C Programming
  • C++ Programming
  • Visual Basic
  • Data Structures
  • Struts 2
  • Java Servlet
  • C# Programming
  • Basic Terms
  • Interviews

World Wide Web

  • Internet
  • Java Script
  • HTML Language
  • Cascading Style Sheet
  • Java Server Pages
  • Wordpress
  • PHP
  • Python Tutorial
  • AngularJS
  • Troubleshooting

 About Us |  Contact Us |  FAQ

Dinesh Thakur is a Technology Columinist and founder of Computer Notes.

Copyright © 2025. All Rights Reserved.

APPLY FOR ONLINE JOB IN BIGGEST CRYPTO COMPANIES
APPLY NOW