Explanation of Partial Class in C#

Explanation of Partial Class in C#

Hello everyone, In this article we are going to talk about Partial Classes in C#. We will talk about why we need partial classes and how can we implement a partial class.

Let's get started.

In C# we can split the class into multiple parts. Generally in huge projects developers are working on same classes simultaneously. Splitting the classes increasing the speed of development in these cases. Developers can use Partial classes to create parts of related classes.

We use partial keyword before class definiton and we use same class names in different files. We can split our class two or more files code files. During the compiling all parts of same class getting together. We can call the class functions normally in program.

You can not define same properties with same names in seperate partial classes. Because even if they are in seperate source files, they are same class. Compiler will throw error as already defined.

Now let's make an example.

In this example, firstly I created a folder and added class source files in this folder. (A namespace has been created.) Then A added class source files in this folder. I created source file for variables, private functions and public functions seperatelly.

Below image you can see it: Partial Classes Source Files
You will see code class contents within below code blocks:
Variables.cs

namespace PartialClassExample.AircraftClass
{
    partial class Aircraft
    {
        private string brand = "Boeing";
        private string majorModel = "777";
        private string minorModel = "300ER";

        public string acToString() => brand + " " + majorModel + "-" + minorModel;
    }
}
InternalMembers.cs

namespace PartialClassExample.AircraftClass
{
    partial class Aircraft
    {
        private void makeUpper() 
        {
            brand = brand.ToUpper();
            minorModel = minorModel.ToUpper();
            majorModel = majorModel.ToUpper();
        }
    }
}
PublicMembers.cs

using System;

namespace PartialClassExample.AircraftClass
{
    partial class Aircraft
    {
        public Aircraft() { }

        public void setAC(string _brand, string _majorModel, string _minorModel)
        {
            this.brand = _brand;
            this.majorModel = _majorModel;
            this.minorModel = _minorModel;
        }

        public void printUpperAC()
        {
            makeUpper();
            Console.WriteLine(acToString());
        }
    }
}
Program.cs

using System;
using PartialClassExample.AircraftClass; //using folder namespace

namespace PartialClassExample
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Title = "Partial Class Example - TheCodeProgram";
            Aircraft ac = new Aircraft();
            Console.WriteLine(ac.acToString());
            ac.printUpperAC();
            ac.setAC("Airbus", "A330", "300");
            Console.WriteLine(ac.acToString());
            ac.printUpperAC();

            Console.ReadLine();
        }
    }
}
Below you can see the example output: C# Partial Class Example Output

As you can see above we can access all members of classes from other parts of class. It s acting like a in same class. It is important that we have to careful about all parts are must be in same namespace.

You can reach the source codes on Github via : https://github.com/thecodeprogram/PartialClassExample

That is all in this article.

Have a good splitting the classes.

Burak Hamdi TUFAN


Tags


Share this Post

Send with Whatsapp

Post a Comment

Success! Your comment sent to post. It will be showed after confirmation.
Error! There was an error sending your comment. Check your inputs!

Comments

  • There is no comment. Be the owner of first comment...