#632 – Partial Methods

A partial method is a method in a partial class that is declared in one part of the class and implemented in another part.

In the example below, we declare a Growl method in part 1 of a class without implementing it.

    // In Dog-1.cs
    public partial class Dog
    {
        public string Name { get; set; }

        public void Bark()
        {
            Console.WriteLine("Woof");
            this.Growl();
        }

        // Partial method declaration
        partial void Growl();
    }

We can then include the implementation of the Growl method in part 2 of the class.

    // Dog-2.cs
    public partial class Dog
    {
        public void Fetch()
        {
            Console.WriteLine("Fetching..");
        }

        // Partial method implementation
        partial void Growl()
        {
            Console.WriteLine("Growling..");
        }
    }

The code in Dog-1.cs is saying that there is a Growl method in the Dog class, but that it will be implemented elsewhere.

About Sean
Software developer in the Twin Cities area, passionate about software development and sailing.