C# Language Reference
partial (Method)

A partial method has its signature defined in one part of a partial type, and its implementation defined in another part of the type. Partial methods enable class designers to provide method hooks, similar to event handlers, that developers may decide to implement or not. If the developer does not supply an implementation, the compiler removes the signature at compile time. The following conditions apply to partial methods:

  • Signatures in both parts of the partial type must match.

  • The method must return void.

  • No access modifiers or attributes are allowed. Partial methods are implicitly private.

The following example shows a partial method defined in two parts of a partial class:

C#
namespace PM
{
    partial class A
    {
        partial void OnSomethingHappened(string s);
    }

    // This part can be in a separate file.
    partial class A
    {
        // Comment out this method and the program
        // will still compile.
        partial void OnSomethingHappened(String s)
        {
            Console.WriteLine("Something happened: {0}", s);
        }
    }
}

For more information, see Partial Classes and Methods (C# Programming Guide).

See Also

Reference

Other Resources

Tags :


Community Content

Stanley Roark
LINQ 2 SQL in MVC tutorial shows partial on method
I had never seen the partial on method. This is similar to C++ where you specify the signature, and then implement it in a .cp file.

Here is the URL to the definitive 195 page (pdf) MVC tutorial for building the NerdDinner: http://weblogs.asp.net/scottgu/archive/2009/03/10/free-asp-net-mvc-ebook-tutorial.aspx

Here's the example of using the "partial" keyword. Note the "partial void OnValidate(ChangeAction action)"

public partial class Dinner {

public bool IsValid {
get { return (GetRuleViolations().Count() == 0); }
}

public IEnumerable<RuleViolation> GetRuleViolations() {
yield break;
}

partial void OnValidate(ChangeAction action) {
if (!IsValid) throw new ApplicationException("Rule violations prevent saving");
}
}

public class RuleViolation {
public string ErrorMessage { get; private set; }
public string PropertyName { get; private set; }

public RuleViolation(string errorMessage) {
ErrorMessage = errorMessage;
}

public RuleViolation(string errorMessage, string propertyName) {
ErrorMessage = errorMessage;
PropertyName = propertyName;
}
}

Note that in the NerdDinner.designer.cs LINQ 2 SQL databased drag-n-drop defined class:
public partial class NerdDinnerDataContext : System.Data.Linq.DataContext

the class has these "extensible" aka partial-method-signatures listed:

#region

Extensibility Method Definitions

partialvoid OnLoaded();

partialvoid OnValidate(System.Data.Linq.ChangeAction action);

partialvoid OnCreated();

partialvoid OnDinnerIDChanging(int value);

partialvoid OnDinnerIDChanged();

partialvoid OnTitleChanging(string value);

partialvoid OnTitleChanged();

partialvoid OnEventDateChanging(System.DateTime value);

partialvoid OnEventDateChanged();

partialvoid OnDescriptionChanging(string value);

partialvoid OnDescriptionChanged();

partialvoid OnHostedByChanging(string value);

partialvoid OnHostedByChanged();

partialvoid OnContactPhoneChanging(string value);

partialvoid OnContactPhoneChanged();

partialvoid OnAddressChanging(string value);

partialvoid OnAddressChanged();

partialvoid OnCountryChanging(string value);

partialvoid OnCountryChanged();

partialvoid OnLatitudeChanging(System.Nullable<double> value);

partialvoid OnLatitudeChanged();

partialvoid OnLongitudeChanging(System.Nullable<double> value);

partialvoid OnLongitudeChanged();

#endregion


I find this odd, that neither myself nor my smartest programmer friend, BJ Herrington, knew about this partial on a method trickery!
ScottGu and friends are Rock-n-Rolla's! Thanks from the community to yalls.

Thanks, LinkedIn.com/danwygant twitter.com/danwygant
Dan Wygant, Sr Software Consultant
MVP 04'-08' Windows Customer Experience
Founder HUNTUG.org VSdotNetUG.org HowToVS.NET
INETA VS.NET User Group Mentor for Al/Ms/La/Tn/Ky

Page view tracker