Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Beginning C# programming with Hello World
The following is a simple "Hello World" program in C# programming. This is typically the first program you write when learning a new language, and it demonstrates the basic structure of a C# application.
Syntax
Following is the basic structure of a C# program −
using System;
namespace NamespaceName {
class ClassName {
static void Main(string[] args) {
// Your code here
}
}
}
Hello World Example
using System;
namespace MyHelloWorldApplication {
class MyDemoClass {
static void Main(string[] args) {
// display text
Console.WriteLine("Hello World");
// display another text
Console.WriteLine("Welcome!");
}
}
}
The output of the above code is −
Hello World Welcome!
Understanding the Code
Let us see now what each part of the program does −
-
using System − The
usingkeyword includes theSystemnamespace in the program. This namespace contains fundamental classes likeConsole. -
namespace declaration − A
namespaceis a collection of classes. TheMyHelloWorldApplicationnamespace contains the classMyDemoClass. -
class − The class
MyDemoClasscontains the data and method definitions that your program uses. Classes generally contain multiple methods. -
Main method − The
Mainmethod is the entry point for all C# programs. It defines what the class does when the program is executed. -
Console.WriteLine() −
WriteLineis a method of theConsoleclass defined in theSystemnamespace. This statement prints the specified text to the console, followed by a new line.
Note − Console.ReadKey() makes the program wait for a key press, preventing the console window from closing immediately when running from Visual Studio. It is not needed when running in an online compiler.
Using String Interpolation
C# also supports string interpolation using the $ prefix, which allows you to embed variables directly inside a string −
Example
using System;
class Program {
static void Main(string[] args) {
string name = "TutorialsPoint";
int year = 2025;
Console.WriteLine($"Welcome to {name} in {year}!");
}
}
The output of the above code is −
Welcome to TutorialsPoint in 2025!
Using Multiple Console Methods
The Console class provides both WriteLine() (prints with newline) and Write() (prints without newline) −
Example
using System;
class Program {
static void Main(string[] args) {
Console.Write("Hello ");
Console.Write("World ");
Console.WriteLine("- using Write()");
Console.WriteLine("This is on a new line - using WriteLine()");
}
}
The output of the above code is −
Hello World - using Write() This is on a new line - using WriteLine()
Write() keeps the cursor on the same line, while WriteLine() moves it to the next line after printing.
Conclusion
Every C# program begins with a Main method inside a class. The using System directive provides access to essential classes like Console. Understanding this basic structure ? namespace, class, Main method, and console output ? forms the foundation for writing all C# applications.
