CLASS

A class is like a container that can store properties, fields, events, method, etc to perform a defined activity. You can create your own custom data types with classes. To access the data within the

A class is like a container that can store properties, fields, events, method, etc to perform a defined activity. You can create your own custom data types with classes. To access the data within the container you would need to create an object of the container.

<access_specifier> class class_name
{
    // Member variables
   <access_specifier> <data_type> variable1;
   <access_specifier> <data_type> variable2;
   
   // Member methods/ Member Functions
   <access_specifier> <return_type> method1(parameter_list)
   {
       // method body
   }
   <access_specifier> <return_type> method2(parameter_list)
   {
       // method body
   }
}

Lets look at some interesting concepts in class:

  • The default access specifier for a class is internal.
  • The default access specifier for a member function or member variable is private.
using System;

namespace ClassUnderstanding
{
    public class AreaCalculator
    {
        //auto properties
        public int length { get; set; }
        public int breadth { get; set; }

        //Default Constructors
        public AreaCalculator()
        {
            Console.WriteLine("Default Cnstructor of Area Calculator");
        }

        //Parameterized Constructors
        public AreaCalculator(int a , int b)
        {
            Console.WriteLine("Welcome to Area Calculator");
            length = a;
            breadth = b;
        }

        //Member Function
        public int Area()
        {
            return (length * breadth);
        }  
    }

    public class InterestCalculator
    {
        //Static auto properties
        public static int p { get; set; }
        public static int t { get; set; }
        public static int r { get; set; }

        //Static Member Function
        public static float interest()
        {
            return (p * t * r) / 100;
        }
    }

    class Program
    { 
        static void Main(string[] args)
        {
            //Calls Default Constructor
            AreaCalculator AreaVariable= new AreaCalculator();

            //Calls parameterized Controller
            AreaCalculator Area1 = new AreaCalculator(5,6);
            int a =Area1.Area();
            Console.WriteLine("Area: {0}",a);

            InterestCalculator.p = 5000;
            InterestCalculator.r = 10;
            InterestCalculator.t = 12;

            float d = InterestCalculator.interest();
            Console.WriteLine("Interest: {0}",d);

            Console.ReadKey();
        }
    }
}

Auto Properties:

Constructor: A class constructor is a special member function of a class that is executed whenever we create new objects of that class. Constructors must have the same name as the class and should not have a return type to it.
They will be called when we create an instance of a class. Constructors can be defined by using an access modifier and class name: <access Specifier> <class name>(){ }

Static Members of a Class: When we declare a member of a class as static, it means no matter how many objects of the class we create there is only one copy of the static member i.e only one instance of the member exists for a class. some facts:

  1. Static variables are used for defining constants. Static variables can be retrieved by creating the instance of a class. So no need of “InterestCalculator variable_name = new InterestCalculator();” instead we can set and retrieve value by “InterestCalculator.P
  2. If we defined a static member function then that function can only access static variables.

We will look more deeply on the above concepts when we work on constructor, static and properties.

4 thoughts on “CLASS

Leave a Reply

Your email address will not be published. Required fields are marked *