What are Partial Classes and Partial Methods in C#?

05 August 2022 at 10:00 by ParTech Media - Post a comment

We all love to do things ‘partially.’ We kickstart many of our day-to-day activities, only to finish them later. Writing blogs, watching movies, and making paintings are classic examples. The same can be achieved in the world of objected-oriented programming, that too with C#!

In object-oriented programming, classes are the fundamental structure on top of which other concepts are built. When you combine the concepts of ‘partial’ and ‘classes’ together, you get ‘partial classes.’ As simple as that.

In this blog, let us explore how to partially create a class and understand the methods that are present inside it.

Table of contents

  1. Introduction to partial classes
  2. Rules of partial classes
  3. Example of partial classes
  4. Introduction to partial methods
  5. Rules of partial methods
  6. Example of partial methods
  7. Conclusion

Introduction to partial classes

Partial classes of C# allow programmers to split binding structures such as class, struct, interface, and method. They are then written in more than one source file. During the compilation time, the split sections are combined and compiled.

Partial classes can be used in scenarios where the developer wants to split the functionality of a class, method, interface, or struct into multiple files. In a large application, a particular class can hold most of the business logic. Splitting the particular class into multiple files enables multiple developers to work on it in parallel. In short, it offers more readability and maintainability.

If any automatically generated source code is added to the project and if the provided class from the source is marked as partial, then developers can add additional functionalities supporting the requirement by using the partial class feature. The best part is - they can do this without disturbing the auto-generated code.

For example, Visual Studio-created classes such as Web Forms and Web Service Wrapper codes are partial classes that can be modified without disturbing the source code created by Visual Studio.

Rules of partial classes

  • Partial classes are identified using the keyword - ‘partial.’ It is expected to be present just before keywords like class, struct, and interface.
  • All the split partial classes should be present inside the same assembly and namespace. Also, they are expected to have the same name.
  • The access specifiers of all the split classes using partial keywords should have the same accessibility (public, private, protected, etc.)
  • If any of the split classes are marked as abstract, sealed, or base, then the whole class is declared of the same type.
  • A partial class can be used in nested class scenarios.
  • Split classes can have different inherited base classes. However, while compiling, all the inherited base classes are considered together.
  • Split classes can have different attributes on top of them. However, during compile time, all the attributes are combined.
  • Partial classes can have partial methods.

Example of partial classes

public partial class PARTECHPartialClass
    {
        public string MyProperty { get; set; }

        public PARTECHPartialClass(string data)
        {
            MyProperty = data;
        }
    }

    public partial class PARTECHPartialClass
    {
        public void Action()
        {
            Console.WriteLine(MyProperty);
        }
    }

In the above code, there are two classes with the same name that are marked as partial. Since you have coded this way, during the compile time, it gets converted to -

public class PARTECHPartialClass
    {
        public string MyProperty { get; set; }

        public PARTECHPartialClass(string data)
        {
            MyProperty = data;
        }

        public void Action()
        {
            Console.WriteLine(MyProperty);
        }
    }

Also, below are some more examples of a partial class -

Nested partial classes

public class NestedClass
    {
        public partial class PARTECHPartialClass
        {
            public string MyProperty { get; set; }
        }

        public partial class PARTECHPartialClass
        {
            public void Action()
            {
                Console.WriteLine(MyProperty);
            }
        }
    }

Attribute merging

[Serializable]
    public partial class Test
    {

    }

    [Obsolete]
    public partial class Test
    {

    }
	

    [Serializable]
    [Obsolete]
    public class Test
    { }

Inheritance in a partial class

public partial class Test : NestedClass
    {

    }

    public partial class Test : IInterface
    {

    }

    public class Test : NestedClass, IInterface
    { }

Introduction to partial methods

Just like a partial class, a partial method is a way to split methods and have them in two different classes. Partial methods can be created with partial classes.

Rules of partial methods

  • Partial methods must have the ‘partial’ keyword and should return only void.
  • It is allowed to have ‘in’ and ‘ref’ parameters but not ‘out’ parameters.
  • It need not have any access specifiers, as it is private by default.
  • It can be static and can also support generic.
  • It doesn’t support virtual, override, sealed, new, or extern.
  • The signatures of the partial method should match in all its occurrences.
  • A delegate can be implemented to the partial method, which has to be used for both declaration and implementation together but not just for declaration.

Example of partial methods

public partial class PARTECHPartialClass
    {
        public string MyProperty { get; set; }
 
        public PARTECHPartialClass(string data)
        {
            MyProperty = data;
        }
 
        partial void Action();
    }
 
    public partial class PARTECHPartialClass
    {
        partial void Action()
        {
            Console.WriteLine(MyProperty);
        }
    }

In the above code, you can see partial classes implemented. Inside both these partial classes, you have the implementation of a partial method. During compile time, they will be stitched together for execution.

Also, it is not mandatory for other partial classes to implement the declared partial methods. This is particularly useful in template coding, where the partial class with the partial method is provided as a template. This allows the developers to implement the required partial methods. When the partial methods are not implemented, then the method declarations and calls to the method are removed during the compilation time. Since they are always of the void return type, even when the partial methods are removed during the compile time, there will be no compile time errors or runtime errors due to it.

Conclusion

And that’s everything you need to know about partial classes and partial methods. In this post, we have also seen the different rules surrounding these two entities in C# along with sample codes to show how partial class and partial method work.

Latest