C# First Application

Let's see the first simple program in C# which prints Hello word on the console.

class FirstExample
{
static void Main (string[] args)
{
System.Console.WriteLine("Hello World ");
}
}
Output
Hello World
Description of the first Program:
  1. class: it’s a keyword which is used to define class. in C#, we have to define class first before main method .
  2. static: It is a keyword which is used to declare the class variable. We don’t need to create object to access static variable .
  3. void: void is a return type of the main method .
  4. Main: In C#, we have to write Main instead of main .
  5. string[] args: It is string array which is used to store the command line arguments. Writing this is optional.
  6. Console.WriteLine("Hello World"): System is the name space. We are writing fully qualified name here. We could have used System name space before. WriteLine is the method used for writing on the console .
C# Example: using System namespace In the first example, if we could have used System namespace then we didn’t need to write fully qualified name to access its method .
using System;
class FirstExample
{
static void Main (string[] args)
{
Console.WriteLine("Hello World ");
}
}
Output
Hello World
C# Example : using public modifier
using System;
public class FirstExample
{
public static void Main (string[] args)
{
Console.WriteLine("Hello World ");
}
}
Output
Hello World
C# Example: using Namespace We can organize our classes and code in a namespace. It is a better way to organize your classes.
using System;
using namespace FirstProgram 
{
public class FirstExample
{
public static void Main (string[] args)
{
Console.WriteLine("Hello World ");
}
}
}
Output
Hello World



ADVERTISEMENT
ADVERTISEMENT