Visual Studio 2010!

Read now >

View Now
DevSource RSS FEEDS
XML Want an easy way to keep up with breaking tech news? And the Get DevSource headlines delivered to your desktop with RSS.
ADVERTISEMENT
ADVERTISEMENT

 

DevSource.com: Your Source for Visual Studio on Facebook
ADVERTISEMENT
Using Partial Methods
By Paul Kimmel

Rate This Article: Add This Article To:

Using Partial Methods
( Page 1 of 2 )

You've seen partial classes when using the Visua Studio form designers. But did you know methodscan be partial? Paul Kimmel reveals some secrets.

Introduction

First the architect created partial classes. Partial classes were deemed good, so next the architect created partial methods.

It just doesn’t work. Trying to phrase partial classes and partial classes like a verse from Genesis just doesn’t work. Let’s try something else.

Partial classes permit developers, including Microsoft developers, to split classes between multiple files. This works out pretty good because it lets Microsoft hide some plumbing, for example, for the Windows Forms designer. Partial classes also makes it possible for programmers that consume generated code to write the custom part in a separate location, in case the generated part needs to be re-generated. So there are definite uses for partial classes.

Partial methods help solve the same kinds of problems as partial classes. With partial methods one developer can define a method and another can implement it. Partial methods can be used to separate custom code, at the method level, from generated code.

In this holiday article I will introduce the basic rules for using partial methods and offer some suggested uses for same. The topic is important and useful but not long (perfect for light holiday reading).

Rules for Partial Methods

Partial methods like any part of a language specification have grammatical rules. In a nutshell here are the things you can do and those that you can’t:

  • Partial methods are preceded by the keyword partial and are implicitly private; because partial methods are private overriding wouldn’t work, so partial methods can’t be virtual
  • Partial methods return void
  • Partial methods can have ref parameters but no out parameters
  • The parameter types and names between partial method declarations and implementations do not have to match
  • Partial methods can have static and unsafe modifiers but cannot be extern
  • Partial methods can be generic
  • Because the semantics for partial methods and delegates are similar, partial methods cannot but delegates

It isn’t necessary to memorize these rules. The help documentation is a great source for checking your facts and the compiler will warn you if a mistake is made. Familiarity is enough; extemporization comes with practice and use frequency. That is, if you use partial methods a lot, like anything else fewer syntactical errors will be made.



 
 
>>> More Microsoft Languages Articles          >>> More By Paul Kimmel