Sharing Code Part IV: Portable Class Libraries

In the last three posts, I have gone through various ways to share common code across the various Windows based platforms. So far in this series of articles in sharing code we have looked at

  1. Sharing Code Part I: Common Source File (source code download)
  2. Sharing Code Part II: Partial Classes (source code download)
  3. Sharing Code Part III: Conditional Compile (source code download)

In this final part, we’ll look at using a Portable Class Library to share the Math2 class across the different platforms instead of the C# source code file. Personally, I prefer sharing the source code file across the platforms and not sharing an assembly as it gives a little more control. Of course, if I’m building out an API or SDK for external developers to use, a Portable Class Library is the way to go.

What is a Portable Class Library?

Essentially a portable class library is DLL that can be shared with developers and has a common set of code that will work on various platforms without modifications.  It can contain business logic or in our example, methods to do some math calculations. You can use a PCL on

  1. .NET Framework
  2. Silverlight
  3. Windows Phone 7
  4. Windows Phone 8
  5. Windows 8 (Store Apps)
  6. XBox 360

and is great for developing an API/SDK to share externally with developers.

Math2 Class

As our Math2 class is essentially our business logic, it’s a good candidate for moving into a Portable Class Library.

Open a Visual Studio Window and create a new Portable Class Library project called Math2


Click OK and in the next dialog select the frameworks you would want to support. In our case we are going to support Windows Phone 7 and higher  and .NET for Windows Store apps.


Click OK and the new project will be created.

Rename Class1.cs to Math2.cs and add the following implementation (same as what we currently have)

namespace Common
    public partial class Math2
        private static Math2 _default;
        public static Math2 Default
                if (_default == null)
                    _default = new Math2();
                return _default;

        private Math2()

        public int Add(int x, int y)
            return x + y;

        public int Subtract(int x, int y)
            return x - y;

        public int Multiple(int x, int y)
            return x * y;

        public int Divide(int x, int y)
            return x / y;

        public float Divide(float x, float y)
            return x / y;

Reference the PCL

There are two ways to reference the Portable Class Library either by including the project in your app solution or by compiling to PCL to release build and referencing the DLL produced.

In our case, we are going to compile in release and reference the assembly in all our projects.

  1. Switch the configuration to Release
  2. Build the Project (Build- Build Solution)

In the WP7_App project,

  1. Delete the Math.cs and Math_WP7.cs file
  2. Click on Project – Add Reference
  3. Click on Browse
  4. Navigate to the location of the Math2 project and location the bin/Release directory
  5. Select the Math2.dll and click Add

Run the project and everything should work as previously.

Do the same to the WP8_App  and W8_App projects, run them and similar to the WP7_App, the apps will run as expected.

Hopefully you got something out of these articles, my son learned a lot especially why i spend so much time in front of my computer. The final code you can download from GitHub.

Warning: count(): Parameter must be an array or an object that implements Countable in /nfs/c07/h02/mnt/110221/domains/ on line 405