Saturday, October 3, 2009

Classes in C#

Introduction :

* C# is a true object oriented language.
* In C# , data items are called fields and the functions are called methods .


Priniples of OOPs:

* we know that all object oriented languages employ three core principles
a) Encapsulation
b) Inheritance
c) Polymorphism


a) Encapsulation :

*It provides the ability to hide the internal details of an object from its users . 
*In C# it is implemented by access modifiers keywords public ,private and protected .
*It is also known as Data Hiding or Information Hiding .

b) Inheritance :

* it is the concept we use to build new classes using existing class definitions.
* the original class is known as base or parent class and the modified one is known as derived class.

c) Polymorphism:

* it is the ability to take more than one form . For example an addition operation involving two numeric values will produce a sum and same addition operation will produce a string if the operands are string values instead of numbers .

DEFINING A CLASS:

the basic form of a class definition :
class classname
{
             [ variable declaration;]
             [ method declaration ;]
}

* class is a keyword , classname is any valid C# identifier .

Note : C++ programmers may note that there is no semicolon after the closing brace .


Adding Variables:

* Data is encapsulated in a class by placing data fields inside the body of the class definition.the variables are called instance  variables .

eg:
class Rectangle
{
         int length;      //instance variable
         int width ;      //instance variable
}

Adding Method: 


General form of a method declaration :

type methodname( parameter -list )
{
       method-body;
}

eg:

class Rectangle
{
        int length;
        int width;
        public void GetData( int x, int y )
        {
                length=x;
                width=y;
        }
}

Note :here note that method has a return type void because it does not return any value .

class Rectangle
{
        int length;
        int width;
        public void GetData( int x, int y )
        {
                length=x;
                width=y;
        }
}
public int RectArea()
{
     int area=length*width ;
     return(area);
}

* Here the new method Rectarea returns result as int.

C# Access Modifiers:

5 types of access modifiers are there:

a) Private :member is accessible only within the class containing the member
b) Public : member is accessible from anywhere outside the class as well.It is also accessible
c) Protected :Member is visible only to its own class and its derived class.
d) Internal : Member is available within the assembly but not to the clients of that component .
e) Protected internal : available in the containing program or assembly and in derived classes .

Note :
1)In C# all members have private access by default .
2)We cannot declare more than one member under a visibilty modifier. for eg:
public:
int x;
int y;
is illegel.

No comments:

Post a Comment