This is a mirror of official site: http://jasper-net.blogspot.com/

Understanding and Using .NET Partial Classes

| Thursday, December 2, 2010
Introduction

One of the language enhancements in .NET 2.0—available in both VB.NET 2005 and C# 2.0—is support for partial classes. In a nutshell, partial classes mean that your class definition can be split into multiple physical files. Logically, partial classes do not make any difference to the compiler. During compile time, it simply groups all the various partial classes and treats them as a single entity.

One of the greatest benefits of partial classes is that it allows a clean separation of business logic and the user interface (in particular the code that is generated by the visual designer). Using partial classes, the UI code can be hidden from the developer, who usually has no need to access it anyway. Partial classes will also make debugging easier, as the code is partitioned into separate files.

In this article, I will examine the use of partial classes in more detail and discuss how Visual Studio 2005 makes use of  partial classes.  

Using the Partial Classes

Listing 1 contains two class definitions written in VB.NET, with the second class definition starting with the partial keyword. Both class definitions may reside in two different physical files. Functionally, Listing 1 is equivalent to Listing 2.

'—MyClass1.Properties.vb
'—one of the classes need not have the Partial keyword
 Public Class MyClass1
        Private pX As Integer
        Private py As Integer

        Property x() As Integer
                 Get
                    Return pX
                 End Get
                 Set(ByVal value As Integer)
                    pX = value
                 End Set
        End Property

Read more: Codeproject

Posted via email from .NET Info

0 comments: