Content by Category
.NET 1.x
.NET 2.0
.NET 3.0
.NET 3.5
.NET 4.0
.NET 4.5
.NET Assemblies
.NET Framework
.NET Getting Started
Accessibility
ADO.NET
Advertorials
Agile Development
AJAX
Amazon Web Services
Analysis Services
Android
Architecture
Arduino
ASP .NET Web API
ASP.NET
ASP.NET MVC
ASP.NET WebForms
Azure
B2B (Business Integration)
BDD
Big Data
Bing
BizTalk
Book Excerpts
Build and Deploy
Business Intelligence
C#
C++
ClickOnce
Cloud Computing
Code Contracts
CODE Framework Info - non Technical
CODE on the Road!
COM+
Community
Conferences
Continuous Integration
Crystal Reports
CSLA.NET
CSS
Data
Debugger
Design Patterns
Development Process
Display Technologies
Distributed Computing
Document Database
DotNetNuke
DSL
Dynamic Languages
Dynamic Programming
Editorials
Enterprise Services ("COM+")
Entity Framework
Events
Expression Blend
F#
Fox to Fox
Frameworks
Functional Programming
Git
Graphics
HTML 5
Internet Explorer 8.0
Interviews
IOS
iPhone
Iron Ruby
Java
Java Script
JavaScript
jQuery
JSON
Lightswitch
LINQ
Linux
LUA
Mac OS X
MDX
Messaging
Metro
Microsoft Application Blocks
Microsoft Business Rules Framework
Microsoft Dynamics
Microsoft Expression
Microsoft Office
Mobile Development
Mobile PC
Mono
MsBuild
MVVM
MySQL
Network
NHibernate
node.js
NOSQL
Nuget
Object Oriented Development
Objective C
Odata
OLAP
Open Source
Opinion
Opinions
Oracle
ORM
Other Languages
Parallel Programming
Patterns
PHP
Podcasts
Post Mortem
PowerPoint
Print/Output
Prism
Product News
Product Reviews
Project Management
Prolog
Python
Q&A
Rails
Rake
Razor
Reporting Services
REST
RIA Services
Ruby
Ruby on Rails
Scheme
Search
Security
Services
SharePoint
SignalR
Silverlight
SOA
Social Networks
Software & Law
Software Business
Source Control
Speech-Enabled Applications
SQL Server
SQL Server 2000
SQL Server 2005
SQL Server 2008
SQL Server 2012
SQL Server CE/AnyWhere/Mobile/Compact
SSIS
Subversion
Sync Framework
Tablet PC
TDD
Team System
Techniques
Testing and Quality Control
TFS
Tips
TypeScript
UI Design
UML
User Groups
VB Script
VB.NET
Version Control
VFP and .NET
VFP and SQL Server
Virtual Earth
Vista
Visual Basic
Visual Basic 6 (and older)
Visual FoxPro
Visual Studio .NET
Visual Studio 11
Visual Studio 2005
Visual Studio 2008
Visual Studio 2010
Visual Studio 2011
Visual Studio 2012
Visual Studio Tools for Office
VSX
WCF
Web Development (general)
Web Services
WebMatrix
WF
Whitepapers
Windows 7
Windows 8
Windows Azure
Windows Live
Windows Phone 7
Windows Phone SDK
Windows Server
Windows Vista
WinForms
WinRT
Workflow
WPF
XAML
Xiine Documentation
XML
XNA
XSLT



LearnNow


XAMALOT
 


Sharepoint TechCon

Reader rating:
Click here to read 8 comments about this article.
Article source: CoDe (2005 - Mar/Apr)


Article Pages: < Previous - 1  2  3 4 5 - Next >


Implications and Repercussions of Partial Classes in the .NET Framework 2.0 (Cont.)

What's a Partial Class, Anyway?

With the .NET Framework 2.0, it is possible to split the definition of a class or an interface over two or more source files. Each source file appears to contain an ordinary class definition from beginning to end. Multiple files in the same project declare the same class but add a distinct fragment of the needed code to the overall definition. In other words, each constituent source file contains a section of the class definition. All parts are then combined together when the application is compiled. Here's a quick example.

public class Person
{
   public string FirstName;
   public string LastName;
   public int Age;
   public DateTime Birth;
}

Today, you need to save the preceding code in the same file and add it to a project. By using partial classes, you can split the above declaration in two or more pieces, each going to a different file. Here's how.

public partial class Person
{
   public string FirstName;
   public string LastName;
}

You save this block of code to a file named, say, Person.Name.cs. Next, you create a second file and name it, say, Person.Birth.cs.

public partial class Person
{
   public int Age;
   public DateTime Birth;
}

With any compiler other than the .NET 2.0 compilers, you get an error. An ambiguous reference to the class Person is at the origin of the error. It would be hard for the compiler to figure out which one is the right Person class to work with. The partial keyword does the trick. It informs the compiler that multiple blocks of code pertaining to the class can be found in various files on the command line. The compiler takes note of that and arranges a tree with the class representation as it finds partial sections of the class in the files on the command line. (See Figure 1.)

Click for a larger version of this image.

Figure 1: The compiler composes a class declaration from two or more distinct source files.

When the compiler processes a class declaration that is characterized by the partial keyword, it adds any member information to an internal table and generates the bytecode for it later in the compile process. As a result, all the members found in the various partial declarations are combined into an all-encompassing class definition. For partial classes to work, it is important that all classes involved sport the same class name.

&


Not Through Distinct Assemblies

Partial classes are a source-level way to extend the behavior of an existing class. Although similar, partial classes have nothing to do with inheritance. For example, you can inherit from a class defined in an external assembly. You can't create a partial class based on the code of a partial class already compiled in a distinct assembly.



Article Pages: < Previous - 1  2  3 4 5 - Next Page: 'Partial Classes Are a Framework Feature' >>

Page 1: Implications and Repercussions of Partial Classes in the .NET Framework 2.0
Page 2: What's a Partial Class, Anyway?
Page 3: Partial Classes Are a Framework Feature
Page 4: Usage of Partial Classes
Page 5: Partial Classes in ASP.NET

How would you rate the quality of this article?
1 2 3 4 5
Poor      Outstanding

Tell us why you rated the content this way. (optional)

Average rating:
3.6 out of 5

39 people have rated this article.

Instantly Search Terabytes Of Text
“Lightning Fast”
– Redmond Mag
“Covers all data
sources” – eWeek
25+ fielded & full-text search options
dtSearch’s own document filters highlight hits in popular file types
Web Spider supports static & dynamic data
APIs for .NET, Java, C++, SQL, etc.
Win / Linux (64-bit & 32-bit)
www.dtSearch.com
 

      LearnNow

 

Sharepoint TechCon