C# Tutorial

C# String

C# Array

C# Flow Control

C# Class and Object

C# Inheritance

C# Interface

C# Collection

C# Generic

C# File I/O

C# Delegate and Event

C# Exception

C# Process and Thread

C# ADO.NET Database Operations

C# Wrapper

In C#, a wrapper is a class or a structure that encapsulates (or "wraps") an object or a functionality, providing a higher level of abstraction, simplicity, or safety. Wrappers can be used for various purposes, such as adapting interfaces, controlling access, or enhancing functionality. In this tutorial, we'll cover how to create a simple wrapper class in C#.

  • Define a scenario:

Let's assume we have a BankAccount class with a Balance property and methods to deposit and withdraw money. Our goal is to create a wrapper class BankAccountWrapper that will limit the maximum amount that can be withdrawn in a single operation.

Here's the BankAccount class:

public class BankAccount
{
    public decimal Balance { get; private set; }

    public void Deposit(decimal amount)
    {
        Balance += amount;
    }

    public void Withdraw(decimal amount)
    {
        if (Balance - amount < 0)
        {
            throw new InvalidOperationException("Insufficient funds.");
        }
        Balance -= amount;
    }
}
  • Create a wrapper class:

Now, let's create a BankAccountWrapper class that will wrap the BankAccount class and limit the withdrawal amount:

public class BankAccountWrapper
{
    private readonly BankAccount _account;
    private const decimal MaxWithdrawalAmount = 1000m;

    public BankAccountWrapper(BankAccount account)
    {
        _account = account;
    }

    public decimal Balance => _account.Balance;

    public void Deposit(decimal amount)
    {
        _account.Deposit(amount);
    }

    public void Withdraw(decimal amount)
    {
        if (amount > MaxWithdrawalAmount)
        {
            throw new InvalidOperationException("Withdrawal amount exceeds the maximum limit.");
        }

        _account.Withdraw(amount);
    }
}

In the BankAccountWrapper class, we have a private field _account that holds a reference to the wrapped BankAccount object. The wrapper exposes the same Deposit and Withdraw methods as the BankAccount class but modifies the behavior of the Withdraw method to enforce a maximum withdrawal amount.

  • Using the wrapper class:

Here's an example of how to use the BankAccountWrapper class:

using System;

namespace WrapperTutorial
{
    class Program
    {
        static void Main(string[] args)
        {
            BankAccount account = new BankAccount();
            BankAccountWrapper wrappedAccount = new BankAccountWrapper(account);

            wrappedAccount.Deposit(1500);
            Console.WriteLine("Account balance after deposit: " + wrappedAccount.Balance);

            try
            {
                wrappedAccount.Withdraw(1200);
            }
            catch (InvalidOperationException ex)
            {
                Console.WriteLine(ex.Message);
            }

            wrappedAccount.Withdraw(500);
            Console.WriteLine("Account balance after withdrawal: " + wrappedAccount.Balance);
        }
    }
}

In this example, we create a BankAccount object and wrap it using a BankAccountWrapper object. We then perform deposit and withdrawal operations using the wrapper, which enforces the maximum withdrawal limit.

This tutorial demonstrates how to create a simple wrapper class in C# and use it to encapsulate and adapt the behavior of another class.

  1. C# wrapper class example:

    • Description: A wrapper class in C# is a class that encapsulates the functionality of another class or system. It often provides a simplified or more convenient interface.
    • Code:
      using System;
      
      public class Calculator
      {
          public int Add(int a, int b)
          {
              return a + b;
          }
      }
      
      public class CalculatorWrapper
      {
          private Calculator calculator;
      
          public CalculatorWrapper()
          {
              calculator = new Calculator();
          }
      
          public int AddNumbers(int num1, int num2)
          {
              // Additional logic can be added here
              return calculator.Add(num1, num2);
          }
      }