What Are Partial Types in C#?

WEBINAR: On-demand webcast

How to Boost Database Development Productivity on Linux, Docker, and Kubernetes with Microsoft SQL Server 2017 REGISTER >

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

  • As all sorts of data becomes available for storage, analysis and retrieval - so called 'Big Data' - there are potentially huge benefits, but equally huge challenges...
  • The agile organization needs knowledge to act on, quickly and effectively. Though many organizations are clamouring for "Big Data", not nearly as many know what to do with it...
  • Cloud-based integration solutions can be confusing. Adding to the confusion are the multiple ways IT departments can deliver such integration...

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date