C# - Syntax
First C# Program
Although the "Hello World!" program looks simple, it has all the fundamental concepts of C# which is necessary to learn to proceed further. Lets break down "Hello World!" program to learn all these concepts.
// Hello World! Example using System; namespace MyApplication { class MyProgram { static void Main(string[] args) { Console.WriteLine("Hello World!"); } } }
The output of the above code will be:
Hello World!
Line 1: This is a single line comment which starts with //. Generally, comments are added in programming code with the purpose of making the code easier to understand. It is ignored by compiler while running the code. Please visit comment page for more information.
Line 2: using System; - added System namespace in the current program. One of the class defined under System namespace is Console which is required for input and output operation in the program.
Line 3: Blank line has no effect. It is used to improve readability of the code.
Line 4: namespace MyApplication - A namespace with name MyApplication is used here to organize codes. It is a container for classes and namespaces.
Line 5, 7, 9, 11, 12 and 13: It consists of open curly bracket { and close curly bracket }. It is essential to keep a block of code within a curly bracket.
Line 6: class MyProgram - C# programming must be done inside a class. Therefore a class called MyProgram is created which is a container for data and methods, and adds functionality to the program.
Line 8: static void Main(string[] args) - It is the Main method of the program and any code inside its curly brackets {} will be executed.
Line 10: Console.WriteLine("Hello World!"); - One of the class defined under System namespace is Console. It contains WriteLine() method which is used here to print "Hello World!" as output. Please note that, in C#, every statement ends with semicolon ;.
Please note that C# is case-sensitive language.
WriteLine() and Write()
There are two methods defined under Console class which can be used to print the output.
- WriteLine(): Output is printed in a new line.
- Write(): Output is printed in the same line.
Inside WriteLine() or Write(), multiple variables can be used, each separated by +.
using System; class MyProgram { static void Main(string[] args) { Console.WriteLine("Hello " + "World!."); Console.WriteLine("Print in a new line."); //prints in the same line Console.Write("Hello " + "World!."); Console.Write(" Print in the same line."); } }
The output of the above code will be:
Hello World!. Print in a new line. Hello World!. Print in the same line.
Semicolons
In a C# program, the semicolon is a statement terminator. Each individual statement in C# must be ended with a semicolon. It indicates that the current statement has been terminated and other statements following are new statements. For example - Given below are two different statements:
Console.WriteLine("Hello " + "World!."); Console.WriteLine("Print in a new line.");
line change, "\n"
To facilitates a line change inside Console.WriteLine() statement or Console.Write() statement, "\n" can be used. "\n" tells to start a new line to the program.
using System; class MyProgram { static void Main(string[] args) { Console.WriteLine("Hello \nWorld!."); Console.Write("Learning C# is fun.\nAnd easy too."); } }
The output of the above code will be:
Hello World!. Learning C# is fun. And easy too.