.NET MAUI › Platform Integration & Handlers

How do you write platform-specific code in shared files?

Beginner

Answer

Use conditional compilation with platform symbols (ANDROID, IOS, MACCATALYST, WINDOWS) inside shared code, or split logic into files under Platforms/ that compile only for their target. Conditional compilation is fine for small snippets; partial classes/interfaces are cleaner for larger differences.
string GetPlatformName()
{
#if ANDROID
    return "Android";
#elif IOS
    return "iOS";
#else
    return "Other";
#endif
}
Real-world example A quick analytics tag reports the OS name using a small #if block rather than a whole new abstraction.

Common follow-ups: When prefer partial classes over #if? | What symbols are available?