#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.

Advertisement

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

5 Responses to #632 – Partial Methods

  1. Pingback: #633 – The Implementation of A Partial Method Is Optional « 2,000 Things You Should Know About C#

  2. Pingback: #634 – Invoking Partial Methods That Have No Implementation « 2,000 Things You Should Know About C#

  3. Pingback: #635 – Limitations on Partial Methods « 2,000 Things You Should Know About C#

  4. Pingback: #636 – The Reason for Partial Methods « 2,000 Things You Should Know About C#

  5. Pingback: #638 – Defining and Using a Partial struct « 2,000 Things You Should Know About C#

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: