Partial classes can be used to

There are many situations when you might need to split a class definition, such as when working on a large scale projects, multiple developers and programmers might need to work on the same class at the same time. In this case we can use a feature called Partial Class.


Introduction to Partial Class

While programming in C# (or OOP), we can split the definition of a class over two or more source files. The source files contains a section of the definition of class, and all parts are combined when the application is compiled. For splitting a class definition, we need to use the partial keyword.

Example 1:

We have a project named as HeightWeightInfo which shows height and weight.

We have a file named as File1.cs with a partial class named as Record. It has two integer variables h & w and a method/constructor named as Record which is assigning the values of h & w.

namespace HeightWeightInfo
{
    class File1
    {
    }
    public partial class Record
    {
        private int h;
        private int w;
        public Record(int h, int w)
        {
            this.h = h;
            this.w = w;
        }
    }
}

Here is another file named as File2.cs with the same partial class Record which has only the method PrintRecord. This method will display the values of h & w.

namespace HeightWeightInfo
{
    class File2
    {
    }
    public partial class Record
    {
        public void PrintRecord()
        {
            Console.WriteLine("Height:"+ h);
            Console.WriteLine("Weight:"+ w);
        }
    }
}

Here now we can see the main method of the project:

namespace HeightWeightInfo
{
    class Program
    {
        static void Main(string[] args)
        {
            Record myRecord = new Record(10, 15);
            myRecord.PrintRecord();
            Console.ReadLine();
        }
    }
}

Here we have the object of the class Record as myRecord which is passing the parameter values as 10 and 15 to h and w respectively to the method defined in File1.cs.

The method PrintRecord is called by the object myRecord which is defined in the File2.cs.

This shows that the partial keyword helps to combine all the attributes of a class defined in various files to work as a single class.

Places where partial class can be used:

  1. While working on a larger projects with more than one developer, it helps the developers to work on the same class simultaneously.
  2. Codes can be added or modified to the class without re-creating source files which are automatically generated by the IDE (i.e. Visual Studio).

Things to Remember about Partial Class

The partial keyword specify that other parts of the class can be defined in the namespace. It is mandatory to use the partial keyword if we are trying to make a class partial. All the parts of the class should be in the same namespace and available at compile time to form the final type. All the parts must have same access modifier i.e. private, public, or so on.

  • If any part is declared abstract, then the whole type is considered abstract.
  • If any part is declared sealed, then the whole type is considered sealed.
  • If any part declares a base type, then the whole type inherits that class.
  • Any class member declared in a partial definition are available to all other parts.
  • All parts of a partial class should be in the same namespace.

**Note: The partial modifier is not available on delegate or enumeration declarations


Introduction to Partial Methods

A partial class may contain a partial method. One part of the class contains the signature of the method. An optional implementation may be defined in the same part or another part. If the implementation is not supplied, then the method and all calls are removed at compile time.

Example 2:

Let's take an example as a partial class

namespace HeightWeightInfo
{
    class File2
    {
    }
    public partial class Record
    {
        public void PrintRecord()
        {
            Console.WriteLine("Height:"+ h);
            Console.WriteLine("Weight:"+ w);
        }
    }
}
4 defined in
namespace HeightWeightInfo
{
    class File2
    {
    }
    public partial class Record
    {
        public void PrintRecord()
        {
            Console.WriteLine("Height:"+ h);
            Console.WriteLine("Weight:"+ w);
        }
    }
}
5 which has three methods InitializeCar(), BuildRim() and BuildWheels(). Among those methods, InitializeCar is defined as partial.

public partial class Car
{
    partial void InitializeCar();
    public void BuildRim() { }
    public void BuildWheels() { }
}

And we have another file named as

namespace HeightWeightInfo
{
    class File2
    {
    }
    public partial class Record
    {
        public void PrintRecord()
        {
            Console.WriteLine("Height:"+ h);
            Console.WriteLine("Weight:"+ w);
        }
    }
}
7 which has two methods BuildEngine and InitializeCar. The method InitializeCar is partial method which is also defined in
namespace HeightWeightInfo
{
    class File2
    {
    }
    public partial class Record
    {
        public void PrintRecord()
        {
            Console.WriteLine("Height:"+ h);
            Console.WriteLine("Weight:"+ w);
        }
    }
}
5.

What is the use of partial classes in C hash?

A partial class is a special feature of C#. It provides a special ability to implement the functionality of a single class into multiple files and all these files are combined into a single class file when the application is compiled.

Why is the partial keyword useful?

The partial keyword indicates that other parts of the class, struct, or interface can be defined in the namespace. All the parts must use the partial keyword. All the parts must be available at compile time to form the final type. All the parts must have the same accessibility, such as public , private , and so on.

What is partial class Mcq?

A partial class is a class whose definition is present in two or more files. Each source file has a section of the class, and all parts are combined when the application is compiled. To split a class definition partial keyword is used as shown in the example below. person class is split into 2 parts.

What is the use of partial class in MVC?

A partial class is one that can be split among multiple physical files. This feature came in C# 2.0. The partial class break the definition of class two or more than two class files, but it will be together at compile time as one class.