What is .NET Partial Classes?
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 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.
Listing 1
'---File1.vb---
Public Class Class1
Public Sub method1()
End Sub
End Class
File2.vb
Partial Public Class Class1
Public Sub method2()
End Sub
End Class
Listing 2
'---File1.vb---
Public Class Class1
Public Sub method1()
End Sub
Public Sub method2()
End Sub
End Class
So, what are the uses for partial classes?
Here are some good reasons to use partial classes:
1. They allow programmers on your team to work on different parts of a class without needing to share the same physical file. While this is useful for projects that involve big class files, be wary: If you find your class file getting too large, it may well signal a design fault and refactoring may be required.
2. The most compelling reason for using partial class is to separate your application business logic from the designer-generated code. For example, the code generated by Visual Studio 2005 for a Windows Form is kept separate from your business logic (we will discuss this in a later section). This will prevent developers from messing with the code that is used for the UI. At the same time, it will prevent you from losing your changes to the designer-generated code when you change the UI.
-
Interview Candidate
- Oct 17th, 2007
- 2
- 4544
Showing Answers 1 - 2 of 2 Answers
Related Answered Questions
Related Open Questions
What is .NET Partial Classes?
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 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.
Listing 1
'---File1.vb---
Public Class Class1
Public Sub method1()
End Sub
End Class
File2.vb
Partial Public Class Class1
Public Sub method2()
End Sub
End Class
Listing 2
'---File1.vb---
Public Class Class1
Public Sub method1()
End Sub
Public Sub method2()
End Sub
End Class
So, what are the uses for partial classes?
Here are some good reasons to use partial classes:
1. They allow programmers on your team to work on different parts of a class without needing to share the same physical file. While this is useful for projects that involve big class files, be wary: If you find your class file getting too large, it may well signal a design fault and refactoring may be required.
2. The most compelling reason for using partial class is to separate your application business logic from the designer-generated code. For example, the code generated by Visual Studio 2005 for a Windows Form is kept separate from your business logic (we will discuss this in a later section). This will prevent developers from messing with the code that is used for the UI. At the same time, it will prevent you from losing your changes to the designer-generated code when you change the UI.
Related Answered Questions
Related Open Questions