Creating classes in C#

When you design a class, you systematically arrange information and behavior into a meaningful entity. A program that uses a class should not have to worry how that class actually works internally; the program simply creates an instance of a class and calls the methods of that class.

✓ Encapsulation – Encapsulation is referred to as information hiding. It actually has two purposes: (1) To combine methods and data within a class, (2) To control the accessibility of the methods and data.

✓ Defining a class – The body of a class contains Area method and radius field.

class Circle
{
    int radius;
    double Area()
    {
        return Math.PI * radius * radius;
    }
}

✓ Creating an intance of class

Circle c; // Create a Circle variable
c = new Circle(); // Initialize it

A class is the definition of a type. An object is an instance of that type, and it is created when the program runs. Several objects can be instances of the same class.

✓ Controlling accessibility – We can modify the definition of a field or method with the public and private keywords to control whether it is accessible from the outside.

A class is created with public method and private field as follows,

class Circle
{
    private int radius;
    public double Area()
    {
        return Math.PI * radius * radius;
    }
}

In above class, though radious is not accessible from outside, it will be available within the circle class. Though radius field can be accessed by Area method, there is no way of initializing it. We can use constructor to fix this.

Though variables declared in a method that are not initialized by default, the fields in a class are automatically initialized to 0, false, or null. However, it is still good practice to provide an explicit means of initializing fields.

✓ Constructors – A constructor is a special method that runs automatically when you create an instance of a class. Every class must have a constructor. If you don’t write one, the compiler automatically generates a default constructor for you.

class Circle
{
    private int radius;
    public Circle() // default constructor
    {
        radius = 0;
    }
    public double Area()
    {  
        return Math.PI * radius * radius;
    }
}

Circle c;
c = new Circle();
double areaOfCircle = c.Area();

Having added a public constructor, you can now use the Circle class and exercise its Area method.

✓ Overloading constructors

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;
    }
}

Circle c;
c = new Circle(45);

If you write your own constructor for a class, the compiler does not generate a default constructor. Therefore, if you’ve written your own constructor that accepts one or more parameters and you also want a default constructor, you’ll have to write the default constructor yourself.

✓ Partial classes – With C#, you can split the source code for a class into separate files so that you can organize the definition of a large class into smaller, easier to manage pieces.

// the contents of cir1.cs file
partial class Circle 
{ 
    public Circle() // default constructor 
    {
        this.radius = 0; 
    }
    public Circle(int initialRadius) // overloaded constructor 
    {
        this.radius = initialRadius; 
    }
}
// the contents of cir2.cs file
partial class Circle 
{
    private int radius; 
    public double Area() 
    { 
        return Math.PI * this.radius * this.radius; 
    }
}

When you compile a class that has been split into separate files, you must provide all the files to the compiler.

✓ Static methods and data – In C#, all methods must be declared within a class. However, if you declare a method or a field as static, you can call the method or access the field by using the name of the class. No instance is required.

Defining a field as static makes it possible for you to create a single instance of a field that is shared among all objects created from a single class.

In the following example, the static field NumCircles in the Circle class is incremented by the Circle constructor every time a new Circle object is created.

class Circle
{
    private int radius;
    public static int NumCircles = 0;
    public Circle() // default constructor
    {
        radius = 0;
        NumCircles++;
    }
    public Circle(int initialRadius) // overloaded constructor
    {
        radius = initialRadius;
        NumCircles++;
    }
}

✓ Creating a static field by using the const keyword – You can declare that a field is static but that its value can never change.

class Math
{
    public const double PI = 3.14159265358979323846;
}

✓ Static classes – A static class cannot contain any instance data or methods, and it does not make sense to try to create an object from a static class by using the new operator.

public static class Math
{
    public static double Sin(double x) {...}
    public static double Cos(double x) {...}
    public static double Sqrt(double x) {...}
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s