Saturday, June 15, 2013

Wrapper class in C#

Let’s learn wrapper class today. Below is the formal bookish definition of Wrapper class.
A wrapper class is any class which "wraps" or "encapsulates" the functionality of 
another class or component. These are useful by providing a level of abstraction from the 
implementation of the underlying class or component;
Not much clear , Right ?
Ok let me give example from my experience. I have a class for TAX calculation containing method to calculate TAX, and my another class called SALARY calculator is there. Now to calculate salary I want to use TAX calculator class. And my interest is that I will not disclose to other class “How I am calculating TAX”?. Then I will simply wrap my TAX calculator class by SALARY class. 
components. Have a look of below code. Though it’s another example one.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Data.SqlClient;
using System.Data;
using System.Diagnostics;

namespace BlogProject
{
    class Wrapper  //Wrapper Class
    {
        class InnerClass  //Inner Class
        {
            public Int32 ProcessData(int Val)
            {
                return Val + 1;
            }
        }

        public Int32 CallRapper()
        {
            Wrapper.InnerClass wpr = new Wrapper.InnerClass(); // Internally call to Wrapper class.
            return wpr.ProcessData(100);
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Wrapper w = new Wrapper();
            Console.WriteLine("Data Process by inner class and show by Wrapper Class:-"+ w.CallRapper());
          
            Console.ReadLine();
        }
    }

}






No comments:

Post a Comment