New Features in C# version 2.0 - Partial Types


The following are the language extensions that added to C# version 2.0.

  • Generics
  • Anonymous Methods
  • Iterators
  • Partial Types
  • Nullable Types

Let us discuss these features one by one. To start of, let us look into partial types with an example from Visual Studio 2005 Beta 1.

Partial Types

Partial types allow classes, structs, and interfaces to be broken into multiple pieces stored in different source files for easier development and maintenance. To define a type in multiple parts, use the modifier 'partial'. It is not a keyword like 'get' and 'set'.

Rules for Partial Types

  • Each part of a partial type declaration must include a partial modifier and must be declared in the same namespace as the other parts.
  • The partial modifier indicates that additional parts of the type declaration may exist elsewhere, but the existence of such additional parts is not mandatory.
  • All parts of a partial type must be complied together, such that the parts can be merged at compile time.

Nested types can be defined in multiple parts by using the partial modifier. As already mentioned partial types can be used with class, structs, and interfaces. The partial modifier is not permitted on delegate or enum declarations.

Partial Type Example

We will first look into a small example, let us create a console application

1. File -> New -> Project -> Console Application
2. Type PartialTypeExample1 in the Name field, click on OK.
3. It will Open Program.cs file
4. Add the modifier 'partial' before the class name. (Look into Figure 1.)

Figure 1. Partial Type

5. Program type has become a partial type.
6. Include a method called Method1( );
7. Instantiate the class and call Method1( );

Figure 2. Partial Type - Method1( ).

8. Go to Solution Explorer, Right Click and Add-> New Item ->C# Class.
9. Name the file as Program1.cs
10. Change the class name to Program from Program1 and include the partial modifier
11. Add a method called Method2( ) it looks as shown in Figure 3.

Figure 3. Partial Type - Method2( ).

12. Switch back to Program.cs and check the Intellisense in the Main method - Figure 4.

Figure 4. Partial Type - Main Method

13. Select Method2 ( ) from the Intellisense and run the application.
14. Check the Output. - Figure 5.

Figure 5. Partial Type - Output

The example could have given you some basic idea to start with partial types. The main advantage of having a partial declaration is to separate the machine generated code and user-written parts of types, so that it is easy to augment code generated by a tool.

This can be explained with a simple example

Assume that you have a tool which has generated a partial type called BankAccount with 2 methods Credit( ) and Debit( ). Refer Figure 6.

Figure 6. Machine Generated Code.

When a developer wants to add some more methods like CheckBalance() and ConvertAccount( ), all needs to be done is, define the partial type and start coding.

Figure 7. User Written Code.

When the partial type BankAccount is bundled for end user, all the methods are available to the end user. This is just a Bird's eye view on partial types. Go on and explore.


Recommended Free Ebook
Similar Articles