Exploring C# 3.0: Listing 1: C#: Separate Code Into Different Files

Partial classes let you separate the code for a class into different files. Prior to Fx 3.5, communication between the classes was limited to events. Fx 3.5 provides partial methods that let you declare a method signature that might or might not be implemented by an adjacent partial class.

public partial class PartialClass
{
public void Save()
   {
      if (Validate())
      {
         // Do Save, return status
      }
   }

   public bool Validate()
   {
      if (!StandardValidation())
      {
         return false;
      }
      bool refValidation = true;
      DoCustomValidation(ref refValidation);
      return refValidation;
   }

   private bool StandardValidation()
   {
      // Test max length, required, etc.
      return true;
   }

   partial void DoCustomValidation(
   ref bool refValidation);
}
comments powered by Disqus
Upcoming Events

.NET Insight

Sign up for our newsletter.

I agree to this site's Privacy Policy.