Hello World in C#
- In this tutorial, you will learn hello world program in c# with explanation using manual and automated with detailed examples. Also alias concepts with examples are given.
MANUAL APPROACH
- Here, user or programmer is responsible for writing c# hello world code, compile the file using csc command and running the file after the successful compilation
- Tools – Notepad
I. EXAMPLE OF HELLO WORLD IN C# VIA NOTEPAD
(WITHOUT USING NAMESPACE)
1. SOURCE CODE
STEP 1: Write the code
(Hello.cs)
class HelloWorld
{
static void Main()
{
System.Console.WriteLine(“Hello World\n”);
}
}
STEP 2: COMPILATION
STEP 3: EXECUTION
2. OUTPUT
NOTE
- Once a predefined namespace (equivalent to c library) is imported, then we can access all the elements of that namespace.
Where,
System : pre-defined namespace
Console : class (class of System namespace)
WriteLine : method (method of Console class)
AUTOMATED APPROACH VIA VISUAL STUDIO IDE
- Here, visual studio IDE will take care of everything like compilation and execution
- Just create project in visual studio 2022 using the steps mentioned here and follow the steps for execution
- Tools – Visual Studio 2022
II. EXAMPLE OF HELLO WORLD IN C#
(USING VISUAL STUDIO 2022 PREVIEW IDE)
1. PROJECT CREATION
2. PROJECT CREATION (Continue)
3. PROJECT CONFIGURATION – SETTING PROJECT NAME AND FRAMEWORK SELECTION
4. SOURCE CODE
(Program.cs)
using System;
namespace Welcome_CS
{
class Program
{
static void Main()
{
System.Console.WriteLine(“Hello World…”);
System.Console.WriteLine(“Good Morning…Welcome”);
}
}
}
5. BUILD THE PROJECT
6. DEBUG THE PROJECT
7. OUTPUT
II. EXAMPLE OF NAMESPACE
(WITH USING NAMESPACE)
1. SOURCE CODE
2. OUTPUT
USING ALIASES FOR NAMESPACE
Existing Problem
- In java, we can load predefined packages as well as class.
- But in .NET, it is not possible to load a particular class directly. We can only import (load) a particular namespace (package).
Solution
- In .NET, it is not possible to directly import classes
- If we want to import particular class in .NET, then we must use the alias concept.
- It can be a user-defined name.
III. EXAMPLE OF ALIAS
1. SOURCE CODE
using p = System.Console; // p is an alias (import class)
namespace AliasApp
{
class Program
{
static void Main()
{
p.WriteLine(“————————————–“);
p.WriteLine(“\tAlias Concept”);
p.WriteLine(“————————————–“);
p.WriteLine(“Welcome to C#…”);
}
}
}
2. OUTPUT
MORE TUTORIALS