In-line Pre-Processor Directives in C#

April 29, 2016

While following this article on how to add in-app purchases to UWP apps, I came across a little quirk of conditional compilation directives. Initially, I started structuring my code like this:



Windows.ApplicationModel.Store.PurchaseResults result;
#if DEBUG
    result = await Windows.ApplicationModel.Store.CurrentAppSimulator.RequestProductPurchaseAsync(Description);
#else
    result = await Windows.ApplicationModel.Store.CurrentApp.RequestProductPurchaseAsync(Description);
#endif

But then I realised, that the code can be split over multiple lines such that the compile directive can sit halfway through the command; like this:




Windows.ApplicationModel.Store.PurchaseResults result = await
#if DEBUG
    Windows.ApplicationModel.Store.CurrentAppSimulator.RequestProductPurchaseAsync(Description);
#else
    Windows.ApplicationModel.Store.CurrentApp.RequestProductPurchaseAsync(Description);
#endif

The MSDN documentation of this can be found here.



Profile picture

A blog about one man's journey through code… and some pictures of the Peak District
Twitter

© Paul Michaels 2024