Hello world
The code
Section titled “The code”namespace HelloWorld{ public class Hello { public static void Main(String[] args) { Console.WriteLine("Hello, World!"); } }}Output
Section titled “Output”Hello, World!How it works
Section titled “How it works”namespace HelloWorld
Section titled “namespace HelloWorld”- A namespace is a way to organize code and avoid name conflicts.
- Essentially, it’s a container for classes and other types.
- Here, we define a namespace called
HelloWorldto contain our program.
{}
- Curly braces
{}are used to define the beginning and end of code blocks. - In short, we put them after we define a namespace, class, function, if statement, or any other code structure.
- Everything inside of them is part of that structure. In this case,
everything between the
{}afternamespace HelloWorldis part of that namespace.
public class Hello
Section titled “public class Hello”- A class is a blueprint for creating objects.
- For now, just think of it as a way to group related code together.
- We define a class named
Helloto hold our program’sMainmethod.
public static void Main(String[] args)
Section titled “public static void Main(String[] args)”- Here, we define (create) a method named
Main. - A method is a block of code that performs a specific task. It’s essentially the same as a function, just that it belongs to a class.
publicmeans that this method can be accessed from outside the class.staticmeans that this method belongs to the class itself, rather than to instances of the class. Basically, we can call it usingHello.Main()without having to create an object of typeHellofirst.voidsays that this method does not return any value.String[] argsis a parameter that allows the program to accept command-line arguments. Here,argsis an array of strings. We’ll get onto function parameters, strings and arrays later on.
What’s special about Main?
Section titled “What’s special about Main?”- The
Mainmethod is the entry point of a C# program. - Basically, it’s the first thing that runs when you start the program.
- Every C# program must have a
Mainmethod to run.
Console.WriteLine("Hello, World!");
Section titled “Console.WriteLine("Hello, World!");”- This line of code prints the text “Hello, World!” to the console (the command line interface).
Consoleis a built-in class in C# that contains methods for working with the console (terminal window).WriteLineis a method of theConsoleclass that outputs text to the console, followed by a new line.- The text to be printed is passed as an argument to the
WriteLinemethod. - The double quotes
"aroundHello, World!tell C# that it’s a string literal (text). - The semicolon
;at the end of the line indicates the end of a statement. We need to put a semicolon at the end of every statement in C#.
I don’t understand…
Section titled “I don’t understand…”- Don’t worry if you don’t fully understand the explanation of the code!
- We will cover all the concepts in detail in other pages.