• Have Any Question?
  • contact@eitworld.com

csharp first program





First C# Program

We have already study Where to write C# code? and How to execute the C# code? in the previous chapters. Now we are going to study about the basic structure of the program in the C# language.
A C# program is basically consists with the following parts:

 

  • NameSpace Declaration
  • Class Name
  • Class data members
  • Class member functions
  • Main method
  • Statements
  • Comments

Let us write our first C# program and see its line by line explanation:

 

using System;
namespace SampleApplication
{
class Sample
{
static void Main()
{ /* my first program in C# */
Console.WriteLine(“C# is new for me…”);
Console.ReadLine();
}
}
}

After execution the above code will produce the following output:

 

Now we are going to explain each line of the program one by one:

 

  • “Using System;”:This line indicates that the program is using the ‘System’ Namespace.
  • “namespace SampleApplication”:This line indicates the declaration of the ‘SampleApplication’ namespace. A namespace is a collection of classes. In our program it contains only one class ‘Sample’.
  • “class Sample”:This line indicates the declaration of the class name ‘Sample’. A class contains any number of member functions and data members. In our program Sample class contains only one method here that is ‘main’.
  • “static void Main()”:This line indicates the declaration of main method. Execution of the all C# programs begins with the main method.
    • Main method begins with ‘static’ keyword and a method that is modified by static can be called before an object of its class has been created.
    • void keyword represents that Main() methods is not returning any value.
    • The empty parentheses that follow Main indicate that no information is passed to Main( ).
  • “/* my first program in C# */” :This line indicates the comment. Comments are those statements of the program which are not executed by the compiler it only gives the description of the program.
  • “Console.WriteLine(“C# is new for me…”);” This line indicates that WriteLine() method belongs to the Console class of System namespace which is used to print the message at the output screen. In our program this method will display the message as “C# is new for me..” at the output screen.
  • “Console.ReadLine();”:This line indicates that ReadLine() method belongs to the Console class which is used to taking input from the user or we can say it is an input function.


Real Time Web Analytics