Introduction to Portable Class Library in .NET Framework 4.5


Application Security Testing: An Integral Part of DevOps

Portable Class Library, otherwise called PCL, comes with .NET framework 4.5 RC and a separate project template is available on Visual Studio 2012 RC. The portable class library can also be installed on top of Visual Studio 2010. It can be downloaded from here.

Portable Class Library

If you are developer working on applications targeting multiple platforms you might have to create duplicates of underlying class libraries targeting the specific platforms, even if they contain the same functionality or logics or algorithms. In .NET framework 4.5 RC, the Portable Class Library has been introduced to solve exactly that problem. By choosing the Portable Class Library template in Visual Studio 2012 RC you can create a single class library to hold the common business logic and share it across applications targeting different platforms like .Net Framework, Windows Phone, Metro style apps, Silverlight and Xbox. Fig 1.0 shows the PCL project template in Visual Studio 2012 RC.

PCL project template in Visual Studio 2012 RC
Fig 1.0: PCL project template in Visual Studio 2012 RC

Selecting the Portable Class Library template will prompt the developer to select different platforms and versions that the library should be supported on. Fig 1.1 shows the platform selection window.

Platform selection window
Fig 1.1: Platform selection window

How it Works?

How ca a single portable class library can be shared across multiple platforms? How will it be compatible? Here is the answer; when you create a Portable Class Library then Visual Studio will take care of importing only the supported set of base class libraries that are compatible with the selected platform versions. Even in the referenced base class libraries, only the supported methods or types will be exposed through intellisense. This set of libraries is wrapped under the project references in the solution explorer as .NET Portable Subset.

.NET Portable Subset
Fig 2.0: .NET Portable Subset

You can use the Visual Studio object explorer to dig through the list of assemblies and the class that they expose.

Visual Studio object explorer
Fig 2.1: Visual Studio object explorer

Even when you explicitly try to perform an Add reference, only the class libraries supporting the selected platform versions will be available.

Only the class libraries supporting the selected platform versions will be available
Fig 2.2: Only the class libraries supporting the selected platform versions will be available

Limitations and Prerequisites

Some limitations or prerequisites of Potable Class Library are:

1. Only a subset of the base class libraries supporting the selected platform versions will be included in the project reference.

2. Below is the list of platforms and versions that support Portable Class Library assemblies.

a. .Net Framework 4.0 or later

b.Silverlight 4 or later

c. Windows Phone 7 or later

d. Xbox 360

3. While deploying an application referencing a Portable Class Library, the target machine or device should have the specific platform version installed, as mentioned in the PCL.

Developing a Sample PCL and Consuming it

In this section we will create a sample Portable Class Library and consume it in a Windows forms application as well as in a Silverlight application.

In Visual Studio 2010, when I tried to add the reference of a normal class library in a Silverlight client application it displayed an error as shown in Fig 3.0.

Visual Studio 2010 error
Fig 3.0: Visual Studio 2010 error

Now in Visual Studio 2012 RC create a Portable Class Library named PortabilityDemoLibrary, add a Windows forms application and a Silverlight application to the same solution. Add a class and interface to the PCL. Below is the code.

namespace PortabilityDemoLibrary
    public interface IValidate
        //Note that generics are allowed in PCL as all the selected platform versions support it.
        bool Validate(List<string> strList);
namespace PortabilityDemoLibrary
    public class Validator : IValidate
        #region IValidate Members
        public bool Validate(List<string> strList)
            //Do some validation on the given string collection.
            return true;

Add the reference of the PCL to the Windows forms application and Silverlight application. Make use of the Validate function from the PCL in both applications. (Note that the adding reference to the Silverlight application goes through fine now).

Window Forms Application Call:

namespace WindowsFormsApplication1
    public partial class Form1 : Form
        public Form1()
            IValidate validator = new Validator();

Silverlight Application Call:

namespace SilverlightApplication1
    public partial class MainPage : UserControl
        public MainPage()
            IValidate validator = new Validator();

Another notable thing is that you can always go and update the platform support for a PCL project at any point in time. Based on the selected changes, Visual Studio will take care of refreshing the PCL project, like adding or removing the supported libraries in the Portable subset as necessary . I hope this article gave a good overview about the Portable Class Library in .Net framework 4.5 RC.

Happy reading!

Related Articles


  • what about portability to Linux/iOS/Android?..

    Posted by Yuri G on 04/11/2013 11:36am

    There is much more expected from Microsoft to fill the gap in mobile and cyber development...

  • Nice article

    Posted by Phani on 08/20/2012 06:29am

    Its very nice article and I like this feature very much.

  • Mr

    Posted by Phani on 08/17/2012 07:03am

    Its a cool feature to be used and good thing it can be used from VS 2010. Its a nice idea to build a DLL to support multiple platoforms. Thank you for this article.

  • You must have javascript enabled in order to post comments.

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • As all sorts of data becomes available for storage, analysis and retrieval - so called 'Big Data' - there are potentially huge benefits, but equally huge challenges...
  • The agile organization needs knowledge to act on, quickly and effectively. Though many organizations are clamouring for "Big Data", not nearly as many know what to do with it...
  • Cloud-based integration solutions can be confusing. Adding to the confusion are the multiple ways IT departments can deliver such integration...

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date
We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.