Thursday, 24 January 2013

Introduction to C # programming language Part 1

              A computer is an electronic device that doesn’t understand human language. It needs some set of instructions to perform any specific task which we call program. Further the program is written in a specific language called programing language that computer understand, C# is one of those programing language to know about the history of C# or programming language you can search on the net or read it in Wikipedia , to read about the history of programming  click here

   we will introduce you to C# after reading the post you will be able to make programs on C# or you can understand it more easily if you are learning C#
   If you think that learning C# require top level of math then you are completely wrong it just require basic mathematics anyone can learn programming if he or she want.
You will require Microsoft visual studio to write programs in C#


    Every programming language require a special program that can translate that programming language into machine language or computer language like I said “ special program that can translate that programming language” every programming language has its own  compiler  example  for a java language you require java compiler , the compiler for C# language is CSC. This process of conversion is called compilation.

Common terms

Before we start making programs there are some common terms which we should know.

Object oriented – programming

     C# is based upon OOP (Object oriented –programming) in this concept the program is developed like a real world system were it represent a real world entity or concept. In this concept a program is chopped into independent and reusable parts called objects, which can be integrated to form a complete program.


         Every object has its own unique identity and it never change in its life time an object may have the same behavior and state example two cars has same behavior accelerate and steer but has its own unique identity like company name or model no.
An object is made up of user defined data and instruction the data stored in an object defined the state of an object now in OOP the data is called attributes and the instruction in an object form its behavior. The behavior of an object defines what it can do, and this behavior is contained in method. An object can have multiple methods, each having a set of instruction. It may be confusing for you now that what make an object to better understand see the image below.   
  Now with the help of this image you can better understand what an object is.                    


        The characteristic of an object like it shape size color behavior classified it into different group. These groups are called classes. The object belonging to a given class is called an instance of the class. A class consists of a set of objects that share a common structure and behavior.

Writing programs

         Like various programming languages, C# also has some predefined keyword. Keywords are reserved words that a special meaning. In programming languages, vocabularies like every human language have, is called keywords and grammar is referred to us as syntax.

Declaring a class

C# also has some predefined classes and method and you can also define your own classes.
The syntax for declaring a class is:

class <class name >

Rules for declaring a class

    1.  Must begin with a letter the first word of the class name must not be a digit (0-9).
    2.  Must not contain any symbol; however an underscore (_) can be use wherever a space is required.
    3.  You can’t use any keyword as class name.

Example to declare a class My_name which show a message “My name is Feroz “you have to write the following code

  public class My_name
       public static void Main()
           System.Console.WriteLine("My name is Feroz");

In this example the class declaration include the Main() method that will display the message My name is Feroz on your screen. 

      1.       The method Main()

              The execution of the code starts from the Main() method. The first line a c# compiler look in a code is the main() method the Main() method is used to create objects and invoke the method of various classes. So you must include a Main method in a class in order to execute your program.

                      2.     System.Console.WriteLine(“My name is Feroz”) 

             The System is a namespace were namespace is a collection of classes if you want to use some input/output operation you have to use System.IO namespace. Console is a class of system namespace. Console class includes a predefined method WriteLine().  WriteLine() method displays the enclosed text on your screen. The Console class also has some more methods that I will introduce to you one by one.The(.)dot character is used to access the WriteLine() method.
      3.    WriteLine()Method.
                     The WriteLine() method is used to write or print text on the screen. After writing the Console.Writeline(“”) statement you can write anything inside the brackets double inverted Quote. 

        4.   The using keyword

                The System.Console.WriteLine() statement can also be written as Console.WriteLine() if the first line of the code include the using keyword. This using keyword is used to include namespace in the program. The using system declares that you can refer to the classes defined in the system namespace. A program can include multiple using statement. Example the previous code can be written as :

          using System;

          public class My_name
              public static void Main(string[] args)

                 Console.WriteLine("My name is Feroz ");



How to run a program

              Now after writing the program your first question will be how to run it, in technical term how to Compile and execute a program to do it you have to first write the program in notepad or any document writer then follow the following step:

Step.1  Save the file with the .cs extension by doing this it will signify as C# program.

Step.2  Now open the visual studio command prompt and change to the directory were you have save the file.

Step.3  Type csc myname.cs to compile the program.

Step.4  Then type  type myname.exe to execute your program.

We have saved our file with myname.cs name that’s why I have used the name in the step don’t be confuse by that

Your program will show the following output on screen.

                                                                                                                                                   next part