C# Programming: Simple OOP IN C#

Friday, October 5, 2012

Simple OOP IN C#

Welcome to my 2nd post on C# programming with a title "Simple OOP IN C#" .So lets begin our project demonstrating a simple OOP(Object Oriented Programming or Paradigm) in C# language.
  Start a new console project in Microsoft Visual Studio or C# express .
File>>New Project>>Visual C#>>Console Application>> Name it "OOP"
 When you finish giving name to the project as default a code editor as below will displayed, as our application will not contain any graphics so only console (ie white text on black screen usually ;) )
So Nothing to design here lets begin to code:

//code of OOP.cpp starts here...
using System;  
 // these are directives ..Notice the S in System its always capital ;) 
 //since c# is a case sensitive language

namespace OOP         //namespace is group of similar classes
{

    class Man           //contains properties or data and functions usually, object of class is used to access them :)
    {
        //properties
        public string name;//if we dont use public it wont be accessible to other classes
        public string address;

        //function
        public void show_info()  //this function will show information
        {
            Console.WriteLine("{0} is from {1}", name, address);

        }
    }
 // thus Man class finished with properties and a function ,can you believe how siple our task gonna be :)..so dont worry lets go ahead.

    //Now we access Man class by creating  object of Man class
    // object usaully is a way to access data( or properties) and functions of any class,thats very simple.
    //still confused on object ..here in our app name and address are data and show_info is a funtion to show those data in screen.Now its clear ya.
    //But why to use object why not directly...its something like taking appointment for meeting the Boss..something legal procedure...

   
    class Program
    {  
        static void Main(string[] args) //static method does not require object to be created  
        {
            //dont bother with 'static' here we will learn about it on other post ..u just need to know is
            //that this Main function is our application's entry point.

            Man obj1 = new Man(); // object of Man class is created here ,object name is obj1
            //while creating new object ,object type must end with braces i.e Man() <--this braces.
            obj1.name = "Yuvaraj ";
            obj1.address = "Butwal";
            Man obj2 = new Man();
            obj2.name = "Alisa";
            obj2.address = "Lumbini";
            obj1.show_info();
            obj2.show_info();
            Console.ReadLine();

 //used for reading line but here used just to hold the screen untill we see the result.
        }
    }
}

//ok now end the functions,classes,namespace with matching curlys.
----------------------------------------------------------------------------------------------------------
click on green play button to see output:
Now we came to end of our project , i hope you guys enjoyed oop programming :)..bye-bye dear..
Note: Working Codes are on blue color  and comments are on green color.


No comments:

Post a Comment