Click here to Skip to main content
16,005,826 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have the following partial method:

public partial class Foo
{
    public virtual partial void PartialMethodFoo();
}

public partial class Foo
{
    public virtual partial void PartialMethodFoo()
    {
        System.Console.WriteLine("Message One.");
    }
}


The above partial method can be overriden with two approaches.

Approach #1

public partial class Bar : Foo
{
    public override partial void PartialMethodFoo();

    public override partial void PartialMethodFoo()
    {
        base.PartialMethodFoo();

        System.Console.WriteLine("\nMessage Two.");
    }
}



Approach #2

public class Bar : Foo
{

    public override void PartialMethodFoo()
    {
        base.PartialMethodFoo();

        System.Console.WriteLine("\nMessage Two.");
    }
}


Both approaches result in overriding the partial method that was marked as virtual. But is there any special difference between both approaches of overriding a partial method?

What I have tried:

It seems like there is no difference but maybe there is something i don't know.
Posted
Updated 27-Oct-21 0:07am
v3

1 solution

Virtual partial methods are new in C# 9. As soon as you add either virtual or override to a partial method, you are required to provide an implementation.

partial method - C# Reference | Microsoft Docs[^]

In your example, both versions will produce the same compiled output. And it doesn't make any sense to use a partial method in a class with only a single part. The only time your first example would make sense would be in a compiler-generated class file, where you wanted to force the user to override the specified method.
C#
// Compiler-generated part:
public partial class Bar : Foo
{
    public override partial void PartialMethodFoo();
}

// User-written part:
public partial class Bar
{
    // Compiler error: must provide implementation of PartialMethodFoo here...
}

NB: The use of partial in the derived class is entirely unrelated to the use of partial on the base class.
 
Share this answer
 
Comments
SleekHustler 27-Oct-21 15:28pm    
Thanks so much for the input. But concerning the last statement you made "The use of partial in the derived class is entirely unrelated to the use of partial on the base class."

Why is that the case?

In the first approach of overriding the partial method, it was necessary to use keyword "partial" to do so. Thus, the use of "partial" in the derived class must be related to the use of "partial" on the base class. right?

Can you further explain what you meant in that statement, as the documentation you linked to made no mention of that. I'd appreciate the further help!
Richard Deeming 28-Oct-21 3:44am    
You can only use partial methods in a partial class.

But the compiled code does not retain the fact that the class or method was declared partial.

You can override a virtual partial method with a non-partial method in a non-partial class. And you can override a non-partial virtual method from a non-partial class with a partial method in a partial class.

The use of partial on the base class or method has no connection to the use of partial on the derived class or method.
SleekHustler 28-Oct-21 21:27pm    
Awesome! thanks for replying back.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900