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 WorldDescription of the first Program:
- class: it’s a keyword which is used to define class. in C#, we have to define class first before main method .
- static: It is a keyword which is used to declare the class variable. We don’t need to create object to access static variable .
- void: void is a return type of the main method .
- Main: In C#, we have to write Main instead of main .
- string[] args: It is string array which is used to store the command line arguments. Writing this is optional.
- 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 .
using System; class FirstExample { static void Main (string[] args) { Console.WriteLine("Hello World "); } }Output
Hello WorldC# Example : using public modifier
using System; public class FirstExample { public static void Main (string[] args) { Console.WriteLine("Hello World "); } }Output
Hello WorldC# 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