What does the portable class library actually solve?

michael picture michael · May 2, 2013 · Viewed 11.7k times · Source

I was wondering, what does the PCL actually solve? If all it does is limit me to what types are cross-platform, then why didn't Microsoft just make this as a feature in a standard .NET library through the IDE?

Basically, I can easily compile a .NET library containing some POCO objects and reference that DLL in my Silverlight, WPF, and Windows Store app without needing to recompile or having any issues. Is there any hard examples of code that works in the PCL that would not work in a standard .NET library?


Oh, and I know that obviously there are some things that would work in the standard .NET library, I'm not concerned about that... I guess my question is this:

Is there any code that would compile in a Portable Class Library, that would not function correclty if that exact same code was in a .NET Library?

Answer

Daniel Plaisted picture Daniel Plaisted · May 3, 2013

Two things:

First, if your intention is to create a library that does work on multiple platforms, you don't want to find out at runtime that you accidentally used an API that wasn't available on all platforms (via a TypeLoadException or MissingMethodException or something). So Portable Class Libraries will only give you intellisense for APIs that are supported on all the platforms you're targeting, and you will get build errors for anything outside that set.

Second, if you create a .NET Framework library that only uses APIs that are available on all platforms, the DLL created still will not work on other platforms (ie Windows Phone and Silverlight) without compiling it as a library for those platforms. It sounds like you expect that it would, and that is a reasonable expectation but has not been true in the past. Portable Class Libraries are the way we are making that work (and in fact it is the case that if you create a class library for Windows Store apps and only use APIs available for the .NET Framework and WP8, the resulting binary would work on both of those platforms unchanged).