1Your First C# Program
Hi friends, in this article we will write our first C# program: Hello World. You will learn how a C# program starts, what Console.WriteLine() does and how to run your code. This is the first practical step towards building applications using C# and .NET.
2Why Do We Start With Hello World?
Before using banking apps, maps or messages, you check whether the phone is working.
Hello World serves a similar purpose in programming. It checks whether your development setup is ready and your first program can execute successfully.
Almost every programming language has a beginner-level Hello World program.
3Understand the Important Parts
1. Console
Console is a .NET class used to perform input and output operations in a terminal.
2. WriteLine()
It displays text and moves the cursor to the next line.
3. String
A string is text enclosed within double quotes, such as "Hello World".
4. Semicolon
A semicolon marks the end of a C# statement.
4Let's Write the Code
Create a console application using the .NET CLI:
dotnet new console -n HelloWorld
cd HelloWorld
Open Program.cs and write the following code:
// My first C# program
Console.WriteLine("Hello World!");
Console.WriteLine("Welcome to C# programming.");
We use Console.WriteLine() twice to display two messages. The blank line in the code is only for readability; it does not produce an empty output line.
5Check the Program Result
Run the application from your terminal:
dotnet run
Hello World! Welcome to C# programming.
Both messages appear on separate lines because WriteLine() moves the cursor to the next line after printing.
6Watch Your First Program Run
Follow the code in the editor and see how the terminal displays each message.
Notice that each WriteLine() statement produces a new line in the terminal.
7What Have We Learned?
You have written and executed your first C# program. You now understand how Console.WriteLine() displays messages and how to run a console application.
- Console.WriteLine() displays text in the terminal.
- Strings are enclosed in double quotes.
- dotnet run executes your C# application.
8Questions Interviewers Love to Ask
What is Console.WriteLine()?
Answer: It is a method that displays text or values in the console and moves the cursor to the next line.
What is the difference between Write() and WriteLine()?
Answer: Write() prints on the current line. WriteLine() prints and moves the cursor to the next line.
Why do we use semicolons in C#?
Answer: A semicolon marks the end of a statement in C#.
Is C# case-sensitive?
Answer: Yes. C# treats uppercase and lowercase identifiers differently. Console and console are not the same.
What is the purpose of Program.cs?
Answer: It is the default source file in a new C# console project. It commonly contains the application's starting code.