TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

C# Partial Types

C# Partial Types for beginners and professionals with examples on overloading, method overriding, inheritance, aggregation, base, polymorphism, sealed, abstract, interface, namespaces, exception handling, file io, collections, multithreading, reflection etc.

<< Back to C-SHARP

C# 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

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

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);
        }
    }
}
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:

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

Next TopicC# Iterators




Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf