In every programming languages, we start programming from the base. A beginner should have complete knowledge of basics. In this c sharp tutorial, we are going to create a "Hello World" program example". There is no shortcut to learn C# programming or other programming languages. You have to focus on all the basics and small terms. Every programming has a different structure to write a program. You should understand this concept and compare with another programming language to become a developer.
First of all, understand the steps to create a "Hello World" program in C# programming.
To compile and execute C# programming, you have to install Visual Studio.Net on your system. If you are already installed Visual Studio.Net then follow the steps -
1. Open your Visual Studio.Net software. You can say .NET framework. The framework reduces programmer efforts.
2. Start visual studio and choose a file->new. It's basic steps, you need to follow any project development.
3. Now Choose Visual C# from templates, and then choose Windows.
4. Here, you have to choose the console Application. All basics examples run on the console application. The console application looks like a black screen programming console where you can create any c# program and compile, execute like c programming.
5. Write your project name. You can specify any name for the console application program.
6. Now you can create a program.
7. If you want to run, click on the Run button. The shortcut key for executing is F5.
Let's create a Hello World program in C# and execute.
using System;
namespace HelloWorldApplication {
class HelloWorld {
static void Main(string[] args) {
Console.WriteLine("Hello World");
Console.ReadKey();
}
}
}
After code compiled and executed, you will get an output like this -
Hello World
Let's understand this example in deep.
1. using system - In this line, using is a keyword and system is a namespace. The program can contain a lot of using system.
2. namespace - A namespace collection of classes like the HeyGoogleApplication namespace contains the class HeyGoogle. This may contain a lot of classes.
3. Class HelloWorld - The class contains a lot of fo method to the process of a program. The methods are created for program behavior. There is one method is main.
4. Main method - This line represents the main definition. Here, we define the main method which is the entry point of C# programming.
5. writeline- The line Console.WriteLine("Hello World"); is used to display the output. WriteLine is a method of the class console.
6.Console.ReadKey- Console.ReadKey() Method makes the program wait for a keypress and it prevents the screen until a key is pressed. The is for the VS.NET Users. This makes the program wait for a keypress and it prevents the screen from running and closing quickly when the program is launched from Visual Studio .NET.
Modification is also simple. You can modify namespace and execute the program.
using System;
namespace Hello{
class HelloWorld {
static void Main(string[] args) {
Console.WriteLine("Hello World");
Console.ReadKey();
}
}
}
In the above example, we changed the namespace and execute the program.
It also gives the same output.
If you forgot to define any namesapce or remove namesapce from C# program then you got the error -
using System;
namespace {
class HelloWorld {
static void Main(string[] args) {
Console.WriteLine("Hello World");
Console.ReadKey();
}
}
}
Compilation failed: 1 error(s), 0 warnings
main.cs(2,10): error CS1001: Unexpected symbol `{', expecting identifier
You should understand the errors. This is a basic program with small code. You should make an error and changes something to explore it.