What Are Partial Types in C#?

Environment: .NET, C# (future)

Partial types are another construct that Microsoft has proposed be added to the C# language in order to allow a single class to be defined in more than one file. Although it is recommended that a class be stored in a single source file, sometimes it is just not practical. While you can rewrite a class to inherit some of the code from a subclass, this also is not always practical.

Partial types resolve this by allowing multiple files to be used to declare a single class. When compiled, the classes can be combined so that a single class is created. One of the most notable benefits of using partial types is when you also use a code generator. If you use a tool to generate some of your code, you can use partial types to combine the generated code with your own additions to the class. You can keep both pieces in separate files. You won't have to worry about inadvertently changing some of the generated code.

Implementing Partial Types

Partial types are implemented using a proposed new C# keyword, partial. The partial keyword is added to the class declaration in each file that will be combined. For example, the following declares a class named MyClass within parts in two different source files named FileOne.cs and FileTwo.cs:

Listing 1—FileOne.cs—Not a Complete Listing

public class partial MyClass
{
   // Class stuff...
   public void FunctionInMyClass()
   {
      // Logic...
   }
   // more stuff...
}

Listing 2—FileTwo.cs—Not a Complete Listing

public class partial MyClass
{
   // Class stuff...
   public void AntherFunctionInMyClass()
   {
      // Logic...
   }
   // more stuff...
}

When these listings are compiled together, the logic is combined into a single class as though a single listing were being used.

About the Author

Bradley Jones is the Executive Editor for Earthweb's Software Development channel, which includes sites such as Developer.com, Codeguru, and Gamelan. He is also the author of the book Sams Teach Yourself the C# Language in 21 Days.



About the Author

Bradley Jones

Bradley Jones, in addition to managing CodeGuru, Brad! oversees the Developer.com Newtwork of sites including Codeguru, Developer.com, DevX, VBForums, and over a dozen more with a focus on software development and database technologies. His experience includes development in C, C++, VB, some Java, C#, ASP, COBOL, and more as well as having been a developer, consultant, analyst, lead, and much more. His recent books include Teach Yourself the C# Language in 21 Days, Web 2.0 Heroes, and Windows Live Essentials and Services.
Google+ Profile | Linked-In Profile | Facebook Page

Comments

  • why cant this be free

    Posted by Legacy on 07/11/2003 12:00am

    Originally posted by: mastereuskilla

    why cant things be free all e_net softwere should be free
    and all others to if you have an answer pleas dont mail me

    Reply
  • Good Information!

    Posted by Legacy on 07/08/2003 12:00am

    Originally posted by: shivpal

    Good Information to get familier with new features!

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: December 11, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT Market pressures to move more quickly and develop innovative applications are forcing organizations to rethink how they develop and release applications. The combination of public clouds and physical back-end infrastructures are a means to get applications out faster. However, these hybrid solutions complicate DevOps adoption, with application delivery pipelines that span across complex hybrid cloud and non-cloud environments. Check out this …

  • With the average hard drive now averaging one terabyte in size, the fallout from the explosion of user-created data has become an overwhelming volume of potential evidence that law-enforcement and corporate investigators spend countless hours examining. Join Us and SANS' Rob Lee for our 45-minute webinar, A Triage and Collection Strategy for Time-Sensitive Investigations, will demonstrate how to: Identify the folders and files that often contain key insights Reduce the time spent sifting through content by …

Most Popular Programming Stories

More for Developers

RSS Feeds