cs6001 C# Unit 2
cs6001 C# Unit 2
cs6001 C# Unit 2
UNIT II
Class, Objects, Constructors and its types, inheritance, properties, indexers, index overloading, polymorphism,
sealed class and methods, interface, abstract class, abstract and interface, operator overloading, delegates, events,
errors and exception, Threading.
______________________________________________________________________
Abstraction
Encapsulation
Polymorphism
Inheritance.
CS600
1
C# and .Net
programming
Page 1
Classes
When you define a class, you define a blueprint for a data type. This does
not actually define any data, but it does define what the class name means.
That is, what an object of the class consists of and what operations can be
performed on that object. Objects are instances of a class. The methods
and variables that constitute a class are called members of the class.
Defining
Class
A class definition starts with the keyword class followed by the class name;
and the class body enclosed by a pair of curly braces. Following is the
general form of a class definition:
<access specifier> class class_name
{
// member variables
<access specifier> <data type> variable1;
<access specifier> <data type> variable2;
...
<access specifier> <data type> variableN;
// member methods
<access specifier> <return type> method1(parameter_list)
{
// method body
}
<access specifier> <return type> method2(parameter_list)
{
// method body
}
...
<access specifier> <return type> methodN(parameter_list)
{
// method body
}
CS600
1
C# and .Net
programming
Page 2
Note:
Access specifiers specify the access rules for the members as well as the class
itself. If not mentioned, then the default access specifier for a class type
is internal. Default access for the members is private.
Data type specifies the type of variable, and return type specifies the data type
of the data the method returns, if any.
To access the class members, you use the dot (.) operator.
The dot operator links the name of an object with the name of a member.
// box 1 specification
Box1.height = 5.0;
CS600
1
C# and .Net
programming
Page 3
Box1.length = 6.0;
Box1.breadth = 7.0;
// box 2 specification
Box2.height = 10.0;
Box2.length = 12.0;
Box2.breadth = 13.0;
// volume of box 1
volume = Box1.height * Box1.length * Box1.breadth;
Console.WriteLine("Volume of Box1 : {0}", volume);
// volume of box 2
volume = Box2.height * Box2.length * Box2.breadth;
Console.WriteLine("Volume of Box2 : {0}", volume);
Console.ReadKey();
}
}
}
When the above code is compiled and executed, it produces the following
result:
Volume of Box1 : 210
Volume of Box2 : 1560
Member
Functions
Encapsulation
and
CS600
1
C# and .Net
programming
Page 4
Let us put above concepts to set and get the value of different class
members in a class:
using System;
namespace BoxApplication
{
class Box
{
private double length; // Length of a box
private double breadth; // Breadth of a box
private double height; // Height of a box public
void setLength( double len )
{
length = len;
}
CS600
1
C# and .Net
programming
Page 5
{
Box Box1 = new Box(); // Declare Box1 of type Box
Box Box2 = new Box();
double volume;
// box 2 specification
Box2.setLength(12.0);
Box2.setBreadth(13.0);
Box2.setHeight(10.0);
// volume of box 1
volume = Box1.getVolume();
Console.WriteLine("Volume of Box1 : {0}" ,volume);
// volume of box 2
volume = Box2.getVolume();
Console.WriteLine("Volume of Box2 : {0}", volume);
Console.ReadKey();
}
}
}
When the above code is compiled and executed, it produces the following
result:
Volume of Box1 : 210
Volume of Box2 : 1560
CS600
1
C# and .Net
programming
Page 6
C#
Constructors
A class constructor is a special member function of a class that is executed
whenever we create new objects of that class.
A constructor has exactly the same name as that of class and it does not
have any return type. Following example explains the concept of
constructor:
using System;
namespace LineApplication
{
class Line
{
private double length; // Length of a line public
Line()
{
Console.WriteLine("Object is being created");
}
C# and .Net
programming
Page 7
line.setLength(6.0);
Console.WriteLine("Length of line : {0}", line.getLength());
Console.ReadKey();
}
}
}
When the above code is compiled and executed, it produces the following
result:
Object is being created
Length of line : 6
A default constructor does not have any parameter but if you need, a
constructor
can
have
parameters.
Such
constructors
are
called parameterized constructors. This technique helps you to assign
initial value to an object at the time of its creation as shown in the following
example:
using System;
namespace LineApplication
{
class Line
{
private double length; // Length of a line
public Line(double len) //Parameterized constructor
{
Console.WriteLine("Object is being created, length = {0}", len);
length = len;
}
CS600
1
C# and .Net
programming
Page 8
{
return length;
}
When the above code is compiled and executed, it produces the following
result:
Object is being created, length = 10
Length of line : 10
Length of line : 6
C#
Destructors
A destructor is a special member function of a class that is executed
whenever an object of its class goes out of scope. A destructor has exactly
the same name as that of the class with a prefixed tilde (~) and it can
neither return a value nor can it take any parameters.
Destructor can be very useful for releasing memory resources before exiting
the program. Destructors cannot be inherited or overloaded.
Following example explains the concept of destructor:
using System;
CS600
1
C# and .Net
programming
Page 9
namespace LineApplication
{
class Line
{
private double length; // Length of a line public
Line() // constructor
{
Console.WriteLine("Object is being created");
}
~Line() //destructor
{
Console.WriteLine("Object is being deleted");
}
CS600
1
C# and .Net
programming
Page
10
When the above code is compiled and executed, it produces the following
result:
Object is being created
Length of line : 6
Object is being deleted
Static Members of a C#
Class
We can define class members as static using the static keyword. When we
declare a member of a class as static, it means no matter how many
objects of the class are created, there is only one copy of the static
member.
The keyword static implies that only one instance of the member exists for
a class. Static variables are used for defining constants because their values
can be retrieved by invoking the class without creating an instance of it.
Static variables can be initialized outside the member function or class
definition. You can also initialize static variables inside the class definition.
The following example demonstrates the use of static variables:
using System;
namespace StaticVarApplication
{
class StaticVar
{
public static int num;
public void count()
{
num++;
}
public int getNum()
{
CS600
1
return num;
C# and .Net
programming
Page
11
}
}
class StaticTester
{
static void Main(string[] args)
{
StaticVar s1 = new StaticVar();
StaticVar s2 = new StaticVar();
s1.count();
s1.count();
s1.count();
s2.count();
s2.count();
s2.count();
Console.WriteLine("Variable num for s1: {0}", s1.getNum());
Console.WriteLine("Variable num for s2: {0}", s2.getNum());
Console.ReadKey();
}
}
}
When the above code is compiled and executed, it produces the following
result:
Variable num for s1: 6
Variable num for s2: 6
You can also declare a member function as static. Such functions can
access only static variables. The static functions exist even before the
object is created. The following example demonstrates the use of static
functions:
using System;
namespace StaticVarApplication
{
CS600
1
C# and .Net
programming
Page
12
class StaticVar
{
public static int num;
public void count()
{
num++;
}
public static int getNum()
{
return num;
}
}
class StaticTester
{
static void Main(string[] args)
{
StaticVar s = new StaticVar();
s.count();
s.count();
s.count();
Console.WriteLine("Variable num: {0}", StaticVar.getNum());
Console.ReadKey();
}
}
}
When the above code is compiled and executed, it produces the following
result:
Variable num: 3
CS600
1
C# and .Net
programming
Page
13
C# Inheritance
One of the most important concepts in object-oriented programming is
inheritance. Inheritance allows us to define a class in terms of another
class, which makes it easier to create and maintain an application. This also
provides an opportunity to reuse the code functionality and speeds up
implementation time.
When creating a class, instead of writing completely new data members and
member functions, the programmer can designate that the new class should
inherit the members of an existing class. This existing class is called
the baseclass, and the new class is referred to as the derived class.
The idea of inheritance implements the IS-A relationship. For example,
mammalIS A animal, dog IS-A mammal hence dog IS-A animal as well,
and so on.
Base
and
Classes
Derived
A class can be derived from more than one class or interface, which means
that it can inherit data and functions from multiple base classes or
interfaces.
The syntax used in C# for creating derived classes is as follows:
<acess-specifier> class <base_class>
{
...
}
class <derived_class> : <base_class>
{
...
}
CS600
1
C# and .Net
programming
Page
14
namespace InheritanceApplication
CS600
1
C# and .Net
programming
Page
15
{
class Shape
{
public void setWidth(int w)
{
width = w;
}
public void setHeight(int h)
{
height = h;
}
protected int width;
protected int height;
}
// Derived class
class Rectangle: Shape
{
public int getArea()
{
return (width * height);
}
}
class RectangleTester
{
static void Main(string[] args)
{
Rectangle Rect = new Rectangle();
Rect.setWidth(5);
Rect.setHeight(7);
CS600
1
C# and .Net
programming
Page
16
When the above code is compiled and executed, it produces the following
result:
Total area: 35
Initializing
Class
Base
The derived class inherits the base class member variables and member
methods. Therefore the super class object should be created before the
subclass is created. You can give instructions for superclass initialization in
the member initialization list.
The following program demonstrates this:
using System;
namespace RectangleApplication
{
class Rectangle
{
//member variables
protected double length;
protected double width;
public Rectangle(double l, double w)
{
length = l;
width = w;
}
CS600
1
C# and .Net
programming
Page
17
{
return length * width;
}
CS600
1
C# and .Net
programming
Page
18
When the above code is compiled and executed, it produces the following
result:
Length: 4.5
Width: 7.5
Area: 33.75
Cost: 2362.5
Multiple Inheritance in
C#
C# does not support multiple inheritance. However, you can use
interfaces to implement multiple inheritance. The following program
demonstrates this:
using System;
namespace InheritanceApplication
{
class Shape
{
public void setWidth(int w)
{
width = w;
}
public void setHeight(int h)
{
height = h;
}
protected int width;
protected int height;
CS600
1
C# and .Net
programming
Page
19
// Derived class
class Rectangle : Shape, PaintCost
{
public int getArea()
{
return (width * height);
}
public int getCost(int area)
{
return area * 70;
}
}
class RectangleTester
{
static void Main(string[] args)
{
Rectangle Rect = new Rectangle();
int area;
Rect.setWidth(5);
Rect.setHeight(7);
area = Rect.getArea();
CS600
1
C# and .Net
programming
Page
20
When the above code is compiled and executed, it produces the following
result:
Total area: 35
Total paint cost: $2450
CS600
1
C# and .Net
programming
Page
21
Understanding Properties in C#
In C#, properties are nothing but natural extension of data fields. They are
usually known as 'smart fields' in C# community. We know that data
encapsulation and hiding are the two fundamental characteristics of any
object oriented programming language.In C#, data encapsulation is possible
through either classes or structures. By using various access modifiers like
private, public, protected, internal etc it is possible to control the
accessibility of the class members.
Usually inside a class, we declare a data field as private and will provide a
set of public SET and GET methods to access the data fields. This is a good
programming practice, since the data fields are not directly accessible out
side the class. We must use the set/get methods to access the data fields.
An example, which uses a set of set/get methods, is shown below.
//SET/GET methods
//Author: [email protected]
using System;
class MyClass
{
private int x;
public void SetX(int i)
{
CS600
1
C# and .Net
programming
Page
22
x = i;
}
public int GetX()
{
return x;
}
}
class MyClient
{
public static void Main()
{
MyClass mc = new MyClass();
mc.SetX(10);
int xVal = mc.GetX();
Console.WriteLine(xVal);//Displays 10
}
}
CS600
1
C# and .Net
programming
Page
23
{
}
set
{
}
}
class MyClass
{
private int x;
public int X
{
get
{
return x;
}
set
CS600
1
C# and .Net
programming
Page
24
{
x = value;
}
}
}
The object of the class MyClass can access the property X as follows.
MyClass mc = new MyClass();
mc.X = 10; // calls set accessor of the property X, and pass 10 as value of
the standard field 'value'.
This is used for setting value for the data member x.
Console.WriteLine(mc.X);// displays 10. Calls the get accessor of the
property X.
The complete program is shown below.
//C#: Property
//Author: [email protected]
using System;
class MyClass
{
private int x;
public int X
{
CS600
1
C# and .Net
programming
Page
25
get
{
return x;
}
set
{
x = value;
}
}
}
class MyClient
{
public static void Main()
{
MyClass mc = new MyClass();
mc.X = 10;
int xVal = mc.X;
Console.WriteLine(xVal);//Displays 10
}
}
Remember that a property should have at least one accessor, either set or
get. The set accessor has a free variable available in it called value, which
CS600
1
C# and .Net
programming
Page
26
CS600
1
C# and .Net
programming
Page
27
{
return x;
}
set
{
x = value;
}
}
}
class MyClient
{
public static void Main()
{
MyClass.X = 10;
int xVal = MyClass.X;
Console.WriteLine(xVal);//Displays 10
}
}
Remember that set/get accessor of static property can access only other
static members of the class. Also static properties are invoking by using the
class name.
Properties & Inheritance
CS600
1
C# and .Net
programming
Page
28
CS600
1
C# and .Net
programming
Page
29
{
public static void Main()
{
Derived d1 = new Derived();
d1.X = 10;
Console.WriteLine(d1.X);//Displays 'Base SET Base GET 10'
}
}
CS600
1
C# and .Net
programming
Page
30
get
{
Console.Write("Base GET");
return 10;
}
set
{
Console.Write("Base SET");
}
}
}
class Derived : Base
{
public override int X
{
get
{
Console.Write("Derived GET");
return 10;
}
set
{
Console.Write("Derived SET");
}
CS600
1
C# and .Net
programming
Page
31
}
}
class MyClient
{
public static void Main()
{
Base b1 = new Derived();
b1.X = 10;
Console.WriteLine(b1.X);//Displays 'Derived SET Derived GET 10'
}
}
Abstract Properties
CS600
1
C# and .Net
programming
Page
32
//Author: [email protected]
using System;
abstract class Abstract
{
public abstract int X
{ ge
t;
set;
}
}
class Concrete : Abstract
{
public override int X
{
get
{
Console.Write(" GET");
return 10;
}
set
{
Console.Write(" SET");
}
}
CS600
1
C# and .Net
programming
Page
33
}
class MyClient
{
public static void Main()
{
Concrete c1 = new Concrete();
c1.X = 10;
Console.WriteLine(c1.X);//Displays 'SET GET 10'
}
}
The properties are an important features added in language level inside C#.
They are very useful in GUI programming. Remember that the compiler
actually generates the appropriate getter and setter methods when it parses
the C# property syntax.
CS600
1
C# and .Net
programming
Page
34
C# Indexers
An indexer allows an object to be indexed such as an array. When you
define an indexer for a class, this class behaves similar to a virtual array.
You can then access the instance of this class using the array access
operator ([ ]).
Synta
x
A one dimensional indexer has the following syntax:
Page
35
}
}
Use
Indexers
of
using System;
namespace IndexerApplication
{
class IndexedNames
{
private string[] namelist = new string[size];
static public int size = 10;
CS600
1
C# and .Net
programming
Page
36
public IndexedNames()
{
for (int i = 0; i < size; i++)
namelist[i] = "N. A.";
}
C# and .Net
programming
Page
37
return ( tmp );
}
set
{
if( index >= 0 && index <= size-1 )
{
namelist[index] = value;
}
}
}
CS600
1
names[0 = "Zara";
]
names[1 = "Riz";
]
= "Nuha";
names[2
names[3 = "Asif";
]
names[4 = "Davinder";
]
C# and .Net
programming
Page
38
names[5] = "Sunil";
names[6] = "Rubic";
for ( int i = 0; i < IndexedNames.size;
{
i+
+
Console.WriteLine(names[i]);
}
Console.ReadKey();
}
}
}
When the above code is compiled and executed, it produces the following
result:
Zara
Riz
Nuh
a
Asif
Davinder
Sunil
Rubic
N. A.
N. A.
N. A.
Overloaded
Indexers
Indexers can be overloaded. Indexers can also be declared with multiple
parameters and each parameter may be a different type. It is not necessary
that the indexes have to be integers. C# allows indexes to be of other
types, for example, a string.
The following example demonstrates overloaded indexers:
using System;
namespace IndexerApplication
{
class IndexedNames
{
private string[] namelist = new string[size];
static public int size = 10;
public IndexedNames()
{
for (int i = 0; i < size; i++)
{
namelist[i] = "N. A.";
}
}
return ( tmp );
}
set
{
if( index >= 0 && index <= size-1 )
{
namelist[index] = value;
}
}
}
public int this[string name]
{
get
{
int index = 0;
while(index < size)
{
if (namelist[index] == name)
{
return index;
}
index++;
}
return index;
}
Zara
Riz
Nuh
a
Asif
Davinder
Sunil
Rubic
N. A.
N. A.
N. A.
2
C# Polymorphism
The word polymorphism means having many forms. In object-oriented
programming paradigm, polymorphism is often expressed as 'one interface,
multiple functions'.
Polymorphism can be static or dynamic. In static polymorphism, the
response to a function is determined at the compile time. In dynamic
polymorphism, it is decided at run-time.
Static
Polymorphism
The mechanism of linking a function with an object during compile time is
called early binding. It is also called static binding. C# provides two
techniques to implement static polymorphism. They are:
Function overloading
Operator overloading
Function
Overloading
You can have multiple definitions for the same function name in the same
scope. The definition of the function must differ from each other by the
types and/or the number of arguments in the argument list. You cannot
overload function declarations that differ only by return type.
The following example shows using function print() to print different data
types:
using System;
namespace
PolymorphismApplication
{
class Printdata
{
void print(int i)
{
Console.WriteLine("Printing int: {0}", i );
}
void print(double f)
{
Console.WriteLine("Printing float: {0}" , f);
}
void print(string s)
{
}
static void Main(string[] args)
{
Printdata p = new Printdata();
Printing int: 5
Printing float: 500.263
Printing string: Hello C++
Dynamic
Polymorphism
C# allows you to create abstract classes that are used to provide partial
class implementation of an interface. Implementation is completed when a
derived class inherits from it. Abstract classes contain abstract methods,
which are implemented by the derived class. The derived classes have more
specialized functionality.
Here are the rules about abstract classes:
using System;
namespace PolymorphismApplication
{
abstract class Shape
{
public abstract int area();
}
class Rectangle: Shape
{
private int length;
private int width;
public Rectangle( int a=0, int b=0)
{
length = a;
width = b;
}
public override int area ()
{
Console.WriteLine("Rectangle class area :");
return (width * length);
}
class RectangleTester
{
static void Main(string[] args)
{
Rectangle r = new Rectangle(10, 7);
double a = r.area();
Console.WriteLine("Area: {0}",a);
Console.ReadKey();
}
}
}
When the above code is compiled and executed, it produces the following
result:
using System;
namespace PolymorphismApplication
{
class Shape
{
protected int width, height;
public Shape( int a=0, int b=0)
{
width = a;
height = b;
}
public virtual int area()
{
Console.WriteLine("Parent class area :");
return 0;
}
}
class Rectangle: Shape
{
public Rectangle( int a=0, int b=0): base(a, b)
{
}
public override int area ()
{
Console.WriteLine("Rectangle class area :");
return (width * height);
}
}
class Triangle: Shape
{
public Triangle(int a = 0, int b = 0): base(a, b)
{
}
public override int area()
{
Console.WriteLine("Triangle class area :");
return (width * height / 2);
}
}
class Caller
{
public void CallArea(Shape sh)
{
int a;
a = sh.area();
Console.WriteLine("Area: {0}", a);
}
}
class Tester
{
{
Caller c = new Caller();
Rectangle r = new Rectangle(10, 7);
Triangle t = new Triangle(10, 5);
c.CallArea(r);
c.CallArea(t);
Console.ReadKey();
}
}
}
When the above code is compiled and executed, it produces the following
result:
C# Interfaces
An interface is defined as a syntactical contract that all the classes
inheriting the interface should follow. The interface defines the 'what' part
of the syntactical contract and the deriving classes define the 'how' part of
the syntactical contract.
Interfaces define properties, methods, and events, which are the members
of the interface. Interfaces contain only the declaration of the members. It
is the responsibility of the deriving class to define the members. It often
helps in providing a standard structure that the deriving classes would
follow.
Abstract classes to some extent serve the same purpose, however, they are
mostly used when only few methods are to be declared by the base class
and the deriving class implements the functionalities.
Declaring
Interfaces
Interfaces are declared using the interface keyword. It is similar to class
declaration. Interface statements are public by default. Following is an
example of an interface declaration:
// interface members
void showTransaction();
double getAmount();
}
Example
The following example demonstrates implementation of the above interface:
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System;
namespace InterfaceApplication
{
public interface ITransactions
{
// interface members
void showTransaction();
double getAmount();
}
t2.showTransaction();
Console.ReadKey();
}
}
}
When the above code is compiled and executed, it produces the following
result:
Transaction: 001
Date: 8/10/2012
Amount: 78900
Transaction: 002
Date: 9/10/2012
Amount: 451900
Abstract classes
Console.WriteLine(dog.Describe());
Console.ReadKey();
}
}
abstract class FourLeggedAnimal
{
public virtual string Describe()
{
return "Not much is known about this
four legged animal!";
}
}
class Dog : FourLeggedAnimal
{
}
}
}
}
Ref :
http://www.tutorialspoint.com/
http://www.csharpcorner.com/UploadFile/tusharkantagarwal/objectorientedcsharp11162005070743AM/objectorientedcsharp.aspx