Partial Types in VS.Net 2005 (Whidbey)

Partial types is a new feature in programming that is introduced in Visual Studio.Net 2005. This feature helps you to divide your types such as class, interface or any type definition across multiple files. At present, when you write any class you need to have the class in a single file, i.e., you need to start the class and end the class in the same file. In VS.Net 2005, you have the option of separating your code for the class across different files. You may split a single class across different files based on functionality, so that you have different developers work on different methods.

There is no modification done in the CLR for partial classes. The implementation of partial classes is done by the compiler for VB.Net and C# in Visual Studio .Net itself. For the CLR, whether you implement the code using partial classes or the class being in a single file, is the same. The compiler, when compiling the code in VS.Net 2005, looks for the partial classes and combines them so that the end result will look as if the code were written in a single class. After compilation the IL produced will look the same for both the implementation.

In Visual Studio .Net 2005, if you try to write some code for an event of a control, in the code-behind file now you will not see the codes that were automatically generated by VS.Net. Instead you will see the code-behind file for an .aspx page which will be a partial class for that page. Since, a class file can be slit across different files, the automatic code generated are placed in a different file which will have a partial class for that .aspx page. Thus, you will see very little code in the code-behind file.

Partial classes are declared using the “partial” keyword. Let us look at the sample code that is given below in C#, which implements a class as partial classes in different files.

//FirstFile.cs
using System;
interface IPartialClass
{
void PrintAString(string s);
void PrintAString();
}

partial class MyPartialClass
{
private string s = "Variable from a separate Partial Class.";
}

class MainClass
{
static void Main(string[] args)
{
MyPartialClass PClass = new MyPartialClass();
PClass.PrintAString("String from a Partial Class.");
PClass.PrintAString();
Console.ReadLine();
}
}

//SecondFile.cs
using System;
public partial class MyPartialClass : IPartialClass
{
public void PrintAString(string str)
{
Console.WriteLine(str);
}
}

//ThirdFile.cs
using System;
partial class MyPartialClass
{
public void PrintAString()
{
Console.WriteLine(s);
}
}

The code given above has three different files, FirstFile.cs, SecondFile.cs, and ThirdFile.cs. The first file has an interface IPartialClass declared in it. This interface is implemented in two other files, SecondFile.cs and ThirdFile.cs. You may note that the method PrintAString(string str) is implemented in the SecondFile.cs and the method PrintAString() is implemented in the ThirdFile.cs. You may note that the string variable ‘s’ is declared in the FirstFile.cs. The IL produced after compiling this code in VS.Net 2005 will look the same as if compiled in earlier versions of VS.Net 2005 and the CLR finds no difference in the IL produced.

If you are not implementing all of the methods in the interface in the partial classes, you will get a compilation error stating that you have not implemented all of the methods. This is the same type of error that you get if you have a single class in a single file.

The PrintAString method in the above example is an overloaded method. A unique overloaded method is implemented in the SecondFile.cs and the ThirdFile.cs respectively. If the signature for the overloaded method in different files does not differ, you will get an error stating that the signature are the same, which is again the same type of error that you get when you have a single class instead of partial classes.

The intellisense in Visual Studio .Net 2005 recognizes the partial class and the implementation of the interfaces across different files. You may note that the private string variable declared in the FirstFile, has the scope in the ThirdFile.cs in which that string variable is actually used.

This type of splitting the class across different files, help in increasing the productivity, as a single big class running into thousands of lines of code can be split into different files. Each of these file can have a different functionality to be developed, which can be given to different developers.






“Amazon and the Amazon logo are trademarks of Amazon.com, Inc. or its affiliates.”

| Privacy Policy for www.dotnet-guide.com | Disclosure | Contact |

Copyright - © 2004 - 2024 - All Rights Reserved.