C# Classes



We have already been using classes in C#. It is now time to learn classes in C# formally. In its most basic form a class in C# consists of some variables and some functions that are to be performed. The function in C# classes are called methods.

We would like to jump start and understand it with an example. Let us assume that we want to define a class called rectangle. The basic idea is that we want to have a variable of this class - it will be its length and breadth. The next thing is - we want to perform some actions on this class. Two possible actions are evaluating its area and perimeter.

Let us jump start and write and run following code in C# Express.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

class Rectangle
{
    int length, breadth;
    int area;    

    
    public void definerectangle(int length_input, int breadth_input)
    {
        length = length_input;
        breadth =  breadth_input;
    }

    
    public int calculatearea()
    {
        area = length*breadth;
        return (area);           
        }

}

namespace ClassRectangle
{
    class Program
    {
        static void Main(string[] args)
        {
            int area1;
            Rectangle rect1 = new Rectangle();
            rect1.definerectangle(10, 20);
            area1 = rect1.calculatearea();
            Console.Write("Area of Rectangle is ");
            Console.WriteLine(area1);
            Console.ReadLine();
        }
    }
}

If you run the above program you should be able to see an output something similar to

	
Area of Rectangle is 200



The concept of this program is simple. We define a class Rectangle. There are two parts of this class. One is a list of the class variable. This class has three variables - length, breadth and area. Take a look at the following code that defines the variables of this class.


class Rectangle
{
    int length, breadth;
    int area;    

|




This part was easy to understant. There nothing very new to it. The thing that makes classes powerful is when we define the methods that can be perfdormed on these classes. Here we define two methods on this class - one is definerectangle and the other is calculatearea.



class Rectangle
{
    int length, breadth;
    int area;    

    
    public void definerectangle(int length_input, int breadth_input)
    {
        length = length_input;
        breadth =  breadth_input;
    }

    
    public int calculatearea()
    {
        area = length*breadth;
        return (area);           
        }

}





The method definerectangle() sets the length and breadth of the rectangle and the method calculatearea() calculates and sets the area.

Creating an Instance of Class

The following statement creates an "instance" rect1 of class Rectangle.

            Rectangle rect1 = new Rectangle();




If we would have liked, we could have created three instance of the class Rectangle.

            Rectangle rect1 = new Rectangle();
            Rectangle rect2 = new Rectangle();
	    Rectangle rect3 = new Rectangle();	

Accessing methods of Class

Here is how we access the methods of the class. The definerectangle(10, 20) sets the length and the breadth of the rectangle.The statement rect1.calculatearea() calculates the area of the instance rect1 of class Rectangle.

            rect1.definerectangle(10, 20);
            area1 = rect1.calculatearea();




We finally print the area of the rectangle using the Console.Writeline. This completes your initial understanding of classes. As an excercise you may like to create a class circle with a variable radius. You will also define two methodss area and perimeter for this class.