using System;
class HelloWorld
{
static void Main()
{
Console.WriteLine("Hello, World!");
}
}
Let’s explain each line of code including keywords.
using System;
Purpose: It enables you to reference the system classes and methods, without having to specify the full name of the thing every time. Without this, you would have to write System.Console.WriteLine whereas you just write Console.WriteLine.
Explanation: This is a using directive and tells the compiler you would like to use System in your program.
Purpose of Keywords:
- using: The using keyword provides you with a mechanism to bring in namespaces into your code without needing to specify the full namespace path every time you will be referencing a class etc.
- System: It is a .NET framework provides namespace contains important classes and base classes. These are the basic operations which are essential for us, these classes include Console, Math, String and a lot of others.
class HelloWorld
{
Purpose: It creates a new class HelloWorld. All the methods in C# has to reside in a class. The main method is stored inside this class.
Explanation: Here this line declares a new class Hello World.
Keywords:
- class: It tells you that you’re declaring a class.
- HelloWorld: The name of the class.
Purpose of Keywords:
- class: When creating a new class, we use the class keyword and define a blueprint of creating objects. A class basically has methods, properties, fields, events etc.
- HelloWorld: The name of the class is this. Typically in C# class names are in PascalCase.
static void Main()
{
Explanation: This is the line declaring the Main method, C# console application entry point.
Purpose of Keywords:
static: With the static keyword we tell that the method is belonging to the class itself and not to the instance of the class. So without creating an object of the class, you can’t call this method.
void: This means that the method doesn’t return any value.
Main: It’s a special method name assigned by the C# compiler that can be used as starting point of the program.
(): You can check this is a method because of the parentheses. In this case Main does not take any parameters but can choose to accept an array of strings (e.g., Main(string[] args)) to pass the command line arguments.
Console.WriteLine("Hello, World!");
Explanation: This is a string that it outputs ‘Hello, World!’ to the console.
Purpose of Keywords and Elements:
Console
: The System is this class which represents the standard input, output, and error streams for console applications..
: Use of the dot operator to access members of a class or namespace.WriteLine
: The Console class has a method this, which writes the specified data to the standard output stream (the console) followed by the current line terminator. When it has written it moves the cursor to the next line.("Hello, World!")
: The argument which will be passed to the WriteLine method is enclosed in parentheses. That string literal in this case is “Hello, World!”. On the console, we will see that.
How It Works: What Console.WriteLine means is the program sends the specified string to the standard output stream and the default is the console window.
}
}
Explanation: And these lines are close to the main method of HelloWorld class.
Purpose:
}: In C# braces {} are are used to specify the scope of a class, method or code block. Each of those respective scopes ends with the closing braces.
- The first closes the Main method.
- The HelloWorld class is closed with the second.
Additional Information
Namespaces: It organizes code and avoid name collisions using namespance. Classes like Console, can be used if you include using System;.
Classes and Objects: This program doesn’t create any object, but knowing that a classes is a blueprint of objects in C# is a must.
Methods: Methods are functions inside classes. It’s special because Main is the entry point.
Static Keyword:
- Static Members: They are rather of the type, than of some specific object;
- Why Main is Static: The class itself needs to be static because the runtime doesn’t create an instance of the class to invoke the Main method.
Void Return Type: It means that the method returns no value to the caller.
Program Execution Flow
Start: As the entry point, our static void Main() is looked for by the runtime.
Execute Main Method: Does the equivalent of Main executing the code.
Call Console.WriteLine: Outputs “Hello, World!” to the console.
End of Main Method: Terminates the Main method with the closing brace }, by reaching.
Program Termination: There’s no more instructions to execute; the program ends its execution.
Summarized the purpose of each keyword
using: It imports a namespace so that we don’t have to add so much typing when we want to access its classes.
class: Declares a new class type.
static: It means that they are part of type themselves rather than of an instance.
void: Indicates that a method has no value returned.
Main: A method of program execution beginning.
Console: A simple console class.
WriteLine: An object that allows writing a line of text to the console.
“: A string literal used to denote a string literal.
;: Statement terminator in C#.
{ and }: Describes the Life of a Code Block, what defines a class, her scope, etc.