Partial Types

C# provides a concept to write source code in separate files and compile it as a single unit. This feature is called partial types and included in C# 2.0. The partial keyword is used to create partial types.

It allows us to write partial class, interface, struct and method in two or more separate source files. All parts are combined when the application is compiled.

Let's see an example. Here, we are creating a partial class that includes a depositeAmount function in the Customer.cs file and a withdraw function in the Customer2.cs file. Both functions are stored in separate file and combined when compiled.

C# Partial Class Example

// Customer.cs

Example

using System;
namespace CSharpFeatures
{
   partial class Customer
    {
        // Deposit function
        public void depositAmount(int d_amount)
        {
            amount += d_amount;
            Console.WriteLine(d_amount+" amount is deposited");
            Console.WriteLine("Available balance is: "+amount);
        }
    }
}

// Customer2.cs

Example

using System;
namespace CSharpFeatures
{
    partial class Customer
    {
        private int amount;
        public int Amount { get => amount; set => amount = value; }
        // Withdraw function
        public void withdraw(int w_amount)
        {
            amount -= w_amount;
            Console.WriteLine(w_amount+" is withdrawn");
            Console.WriteLine("Available balance is: "+amount);
        }
    }
}
Example

using System;
namespace CSharpFeatures
{
    class Program
    {
        static void Main(string[] args)
        {
            Customer customer = new Customer();
            customer.Amount = 2000;
            Console.WriteLine("Current balance is: "+ customer.Amount);
            customer.depositAmount(1000);
            // Accessing seperate file function
            customer.withdraw(500);
        }
    }
}

Output:

Output

Current balance is: 2000
amount is deposited
Available balance is: 3000
500 is withdrawn
Available balance is: 2500

Input Required

This code uses input(). Please provide values below: