Module 2
Module 2
■ To combine methods and data within a class; in other words, to support classification
■ To control the accessibility of the methods and data; in other words, to control the use of the class
• Defining and using a class
• In C#, we use the class keyword to define a new class. The data and methods of the class occur in the
body of the class between a pair of braces. Following is a C# class called Circle that contains one
method (to calculate the circle’s area) and one piece of data (the circle’s radius):
class Circle
{
int radius; double Area()
{
return Math.PI * radius * radius;
}
}
We create a variable specifying Circle as its type, and then we initialize the variable
with some valid data. Here is an example:
Circle c; //
Create a Circle variable
c = new Circle(); // Initialize it
We cannot write a statement such as this because it gives an error: Circle
c;
c = 42;
We can directly assign an instance of a class to another variable of the same type, like
this: Circle c;
c = new Circle();
Circle d;
d = c;
Controlling accessibility
■ A method or field is private if it is accessible only from within the class. To declare that a
method or field is private, we write the keyword private before its declaration. As intimated
previously, this is actually the default, but it is good practice to state explicitly that fields
and methods are private to avoid any confusion.
■ A method or field is public if it is accessible from both within and outside of the class. To
declare that a method or field is public, we write the keyword public before its declaration.
class Circle
{
private int radius;
public double Area()
{
return Math.PI * radius * radius;
}
radius is declared as a private field and is not accessible from outside the class, radius is accessible from within
the Circle class. The Area method is inside the Circle class, so the body of Area has access to radius.
• Working with constructors
A constructor is a special method that runs automatically when we create an instance of a class. It has the same
name as the class, and it can take parameters, but it cannot return a value (not even void). Every class must have
a constructor. If we don’t write one, the compiler automatically generates a default constructor for us.
class Circle
{
private int radius;
public Circle() // default constructor
{
radius = 0; } public double Area()
{
return Math.PI * radius * radius;}
}
We use dot notation to invoke the Area method on a Circle object: Circle
c;
c = new Circle();
double areaOfCircle = c.Area();
• Overloading constructors
Overloading constructor is a constructor where parameters are passed to it is called overloaded constructor.
A constructor is just a special kind of method and it—like all methods—can be overloaded. We can add
another constructor to the Circle class, with a parameter that specifies the radius to use, like this:
class Circle
{
private int radius;
public Circle() // default constructor
{
• radius = 0;
}
public Circle(int initialRadius) // overloaded constructor
{
• radius = initialRadius;
}
public double Area()
{
return Math.PI * radius * radius;
}
}
Partial classes
• A class can contain a number of methods, fields, and constructors, as well as other items
• We can split the source code for a class into separate files so that we can organize the definition of a
large class into smaller, easier to manage pieces.
• When we split a class across multiple files, we define the parts of the class by using the partial
keyword in each file.
For example:
partial class Circle
{
public Circle() // default constructor
{ this.radius = 0;
}