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

  • Data integrity and ultra-high performance dictate the success and growth of many companies. One of these companies is BridgePay Network Solutions, a recently launched and rapidly growing financial services organization that allows merchants around the world to process millions of daily credit card transactions. Due to the nature of their business, their IT team needed to strike the perfect balance between meeting regulatory-mandated data security measures with the lowest possible levels of latency and …

  • How Red Hat Enterprise Linux shrinks total cost of ownership (TCO) compared to Windows IT organizations face the constant challenge of juggling two almost opposing priorities: continuously delivering business-critical application services while keeping IT expenses in line with budget constraints. The primary function of IT departments is to supply core infrastructure and applications to attract new business, generate revenue, and facilitate profitability - and IT managers strive to meet this goal in spite of …

Most Popular Programming Stories

More for Developers

RSS Feeds

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