Hello, friends of the coding world! How are you? I hope you are well. Today, we will take a look at that magical moment when we all took our first excited steps, our very first C# program. You know, that famous ‘Hello World’, and today we are chasing after it. Honestly, when I first saw the topic, it reminded me of my first coding lessons. Back then, everything seemed so complicated, didn’t it? Of course, this initial stage isn’t always like this; sometimes, you need to make a start.
Now, when we first meet C#, the first thing that comes to mind is that ‘Hello World!’ message. Why ‘Hello World’? I think it has a simple reason: seeing what you wrote appear on the computer screen in a very gratifying way is so motivating to see for the first time. It’s like you ‘talked’ to the computer for the first time. Isn’t that wonderful?
So, how does this ‘Hello World’ program manage to display this message on the screen? Here, we need to delve a little into the structure of the code. The basic structure isn’t that complicated. First, C# codes are generally contained within a namespace. You can think of this namespace as a kind of folder system that helps organize your code. Then, every C# application must have a ‘Main’ method. This ‘Main’ method is the starting point of the program. When your computer runs the program, it looks here first.
By the way, you’ll see a ‘class’ right above the ‘Main’ method. Since C# is an object-oriented language, everything runs inside classes. So, you could say all your code lives within one or more classes. You will see this more clearly in the code I will show shortly.
Anyway, without prolonging the story, let’s take a look at the famous ‘Hello World’ program. You know, at first, we all make mistakes, like I couldn’t run mine initially; I kept getting error messages on the screen, and got a bit frustrated. Luckily, a friend helped me fix it. This code is actually one of the simple versions my friend showed me.
The code that will be reflected on the screen accomplishes its task with just one line. There is a command called System.Console.WriteLine(). This command takes everything you write inside the parentheses and prints it on the computer screen, that is, on the console. It’s that simple. When you put ‘Hello World!’ inside the quotes in the parentheses, that magical moment happens. Having ‘public static void Main(string[] args)’ before this command is also necessary for the program to execute this command.
This ‘public static void Main’ part can be a bit confusing at first, and I didn’t understand it either, but roughly, we can think of it like this: ‘public’ means this method is accessible from anywhere. ‘static’ means this method can run without creating an object. ‘void’ means this method doesn’t return any value. ‘string[] args’ allows you to receive some information from outside when starting the program, but for our first program, we don’t need this.
Now, let’s look at that simple but very important code example. Sometimes, a small mistake in your code can prevent it from working, and those moments can be frustrating. I initially thought it was a trivial thing, but when I got into it, I realized that having a solid foundation makes building upon it much easier.
Here’s the basic example for your first C# program:
using System; namespace HelloWorldApp { class Program { static void Main(string[] args) { Console.WriteLine("Hello World!"); } } }
As you can see, it’s not very complicated. The ‘using System;’ line includes a library needed to use the Console class. Then, with ‘namespace HelloWorldApp’, we create our own namespace. Next, we define a class with ‘class Program’, and finally, within the ‘static void Main(string[] args)’ method, we run the command ‘Console.WriteLine(“Hello World!”);’. That’s all.
At first, I wrote this code a little differently. Like omitting curly braces or forgetting the semicolon, common mistakes. For example, I did this at the beginning:
// WRONG EXAMPLE (It gives an error)
using System; namespace HelloWorldApp { class Program { static void Main(string[] args) { Console.WriteLine("Hello World!") // Missing semicolon! } } }
Yeah, a beginner coder like me could make such a mistake. And then spends hours wondering why it doesn’t work. I think to avoid these simple errors, it’s always good to make a checklist. Anyway, the correct way is already shown above, which is more important.
By the way, this ‘Console.WriteLine’ command isn’t just for writing text; it can also display numbers. It can even perform mathematical operations and display the result. For example:
Console.WriteLine(5 + 3); // Displays 8 on the screen.
Isn’t that great? Seeing what this simple command can do motivates people. Maybe that’s why this ‘Hello World’ example is the first taught in every programming language, who knows?
In conclusion, you have written your first C# program ‘Hello World’ and understood its basic structure. This is a huge step in your coding journey. Remember, every expert was once a beginner. The important thing is to try, make mistakes, and not give up. By the way, you can try this code in an environment like Visual Studio and display your own ‘Hello World’ message on the screen. I’m sure you will really enjoy it.