In C# programming language, a simple "hello world" program can be written by multiple ways. Let's see the top 4 ways to create a simple C# example:
C# Simple Example
class Program
{
static void Main(string[] args)
{
System.Console.WriteLine("Hello World!");
}
}
Output:
Hello World!
C# Example: Using System
If we write using System before the class, it means we don't need to specify System namespace for accessing any class of this namespace. Here, we are using Console class without specifying System.Console.
using System;
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
}
}
Output:Hello World!
C# Example: Using public modifier
We can also specify public modifier before class and Main() method. Now, it can be accessed from outside the class also.
using System;
public class Program
{
public static void Main(string[] args)
{
Console.WriteLine("Hello World!");
}
}
Output:Hello World!
C# Example: Using namespace
We can create classes inside the namespace. It is used to group related classes. It is used to categorize classes so that it can be easy to maintain.
using System;
namespace ConsoleApplication1
{
public class Program
{
public static void Main(string[] args)
{
Console.WriteLine("Hello World!");
}
}
}
Output:Hello World!
- Simple Example
- Using System
- Using public modifier
- Using namespace
C# Simple Example
class Program
{
static void Main(string[] args)
{
System.Console.WriteLine("Hello World!");
}
}
Output:
Hello World!
C# Example: Using System
If we write using System before the class, it means we don't need to specify System namespace for accessing any class of this namespace. Here, we are using Console class without specifying System.Console.
using System;
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
}
}
Output:Hello World!
C# Example: Using public modifier
We can also specify public modifier before class and Main() method. Now, it can be accessed from outside the class also.
using System;
public class Program
{
public static void Main(string[] args)
{
Console.WriteLine("Hello World!");
}
}
Output:Hello World!
C# Example: Using namespace
We can create classes inside the namespace. It is used to group related classes. It is used to categorize classes so that it can be easy to maintain.
using System;
namespace ConsoleApplication1
{
public class Program
{
public static void Main(string[] args)
{
Console.WriteLine("Hello World!");
}
}
}
Output:Hello World!
Post a Comment