Multi-targeting
Build one library for multiple target frameworks from a single project; use conditional compilation and fallback shims for API differences.
Multi-targeting is a free C# Academy lesson on CoddyKit — lesson 3 of 3. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C# Academy learning path, one of 3 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Concept & benefits
Goal: Ship one package that works on multiple platforms.
- Single project produces multiple binaries
- Consumers get the best-fit assembly automatically
- Use small #if guards when APIs differ
Asset selection basics
- Package contains folders per target (e.g., lib/net45, lib/netstandard2.0)
- NuGet picks the nearest compatible asset
- Keep public API consistent; only internal code should differ
Conditional compilation demo
Use small #if blocks to vary implementation. Keep the public method signature identical.
#define NET45 // simulate building for different targets
// #define NETSTANDARD2_0
using System;
public class Program
{
public static void Main(string[] args)
{
Console.WriteLine("Target: " + TargetName());
}
// Public API: stays the same across targets
public static string TargetName()
{
#if NET45
return "net45 build";
#elif NETSTANDARD2_0
return "netstandard2.0 build";
#else
return "unknown target";
#endif
}
}
Stable API, internal shims
Keep the same public API; hide differences behind small shims so callers do not care which target is used.
#define NET45
using System;
using System.IO;
public class Program
{
// Public API is stable; internal implementation differs.
public static string ReadAllTextCompat(string path)
{
#if NET45
// net45 has File.ReadAllText; use it directly.
return File.ReadAllText(path);
#else
// Example shim path (for an older TF that lacks some helper API)
using (var fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read))
using (var sr = new StreamReader(fs))
{
return sr.ReadToEnd();
}
#endif
}
public static void Main(string[] args)
{
// For demo, write then read
string p = "demo.txt";
File.WriteAllText(p, "hello");
Console.WriteLine(ReadAllTextCompat(p));
}
}
Setup tips
- Use TargetFrameworks (plural) in the project to list targets
- Limit #if to tiny helpers; keep most code shared
- Test each target; run unit tests per TFM
- Publish one package with all target assets
Common pitfalls
- Do not expose different public APIs per target
- Avoid deep #if nests; use small shims
- Watch for implicit behavior differences (paths, encodings)
- Document any target-specific limitations
Why multi-targeting
Recap
Recap: Multi-targeting ships several binaries from one project. Keep the public API consistent, hide differences behind tiny #if shims, and package all targets together.
Frequently asked questions
Is the “Multi-targeting” lesson free?
Yes — the full text of “Multi-targeting” is free to read here on the web, and the C# Academy course includes 3 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C# Academy course, upgrade to CoddyKit PRO.
What will I learn in “Multi-targeting”?
Build one library for multiple target frameworks from a single project; use conditional compilation and fallback shims for API differences. You practise C# Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start C# Academy?
No prior experience is required. C# Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 3, so you can start here or from the beginning and move at your own pace.
How long does the “Multi-targeting” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this C# Academy lesson?
Yes. Every C# Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.