When C# is the right choice, and when it is not
The other pages of this group compare one task at a time; this one answers the question the group exists for. The short answer is that neither language wins, and that the honest reason to choose one is rarely the language: it is the platform the program has to run on, the team that has to maintain it, and the library that already solves the problem. The rest of the page is the long answer, and it names the cases where each language is the wrong choice.
What each is good at
| Area | C# | Java |
|---|---|---|
| Type system | records, record structs, structs, nullable reference types, pattern matching with list patterns | records, sealed interfaces, record patterns, no value classes yet |
| Generics | reified: the type argument exists at run time, and a list of int stores ints | erased: one class per generic type, and a primitive needs a wrapper |
| Asynchrony | async and await in the language, Task in the library | CompletableFuture in the library, virtual threads since Java 21 |
| Queries | LINQ with two syntaxes and deferred execution | the Stream API with collectors |
| Desktop | WinForms, WPF, MAUI for Windows; Unity for games | JavaFX, Swing; Android is the large one |
| Server | ASP.NET Core, one of the fastest web stacks in the benchmarks | Spring, Quarkus, the largest enterprise ecosystem |
| Runtime | CoreCLR with generational GC, tiered JIT and NativeAOT | HotSpot with several collectors, C1 and C2, GraalVM native image |
| Portability | Windows, Linux and macOS; the SDK is one download | every platform with a JVM, which is more of them than .NET has |
When Java is better
- The program has to run on a platform where .NET is not supported, or on a device where the JVM already is. Android is the large case: it is Java and Kotlin, and no C# runtime is a first-class citizen there.
- The team, the libraries and the hiring pool are Java. Spring, Hibernate, Kafka's client, most of the data tooling and most of the enterprise code in the world are Java, and a rewrite into C# is a cost with no feature behind it.
- The build has to be one artifact for a hundred machines with different runtimes: a jar and a JRE are the oldest answer to that, and every operations team has run one.
- A measured difference is not a reason. On this container the loop of the throughput page was 122.6 against 129.8 milliseconds, and the page says that is inside the noise; a project that chooses a language for five percent of a loop has chosen for the wrong thing.
- Checked exceptions, which this course calls a cost, are a feature for a library that must make its failures impossible to ignore. A Java API that throws IOException says so in its signature, and every caller has decided what to do.
When C# is better
- The program is for Windows: a desktop tool, a service, an add-in for Office or a game in Unity. The Windows UI stacks of this site are C# and have no Java equivalent of the same quality.
- The program is a performance-sensitive server or a tool with a large allocation budget: structs, Span<T>, reified generics and ref struct give a C# program control that Java hands to the JIT and the wrapper classes, and AsyncAOT gives a native executable without a runtime.
- The team wants one toolchain: dotnet is the compiler, the package manager, the test runner and the publisher, and it installs as one package. A Java project picks a JDK, a build tool, a test framework and a container; the choice is freedom and it is work.
- The code style of the team fits the language: properties, LINQ, records with with, and warnings that are errors by policy. A team that writes C# well writes less code for the same behaviour than the Java page of this group shows, and the pages here are the evidence of how much.
- The program is a script-sized tool that must start fast: a NativeAOT publish of a console program starts in milliseconds, where a JVM pays its startup every time. The startup page measured 71 against 53 milliseconds for a hello program, which is the managed-runtime price, not the floor.
Tools, deployment and onboarding
The build page measured the two toolchains in this container: a C# framework-dependent publish of a small program is 128 KiB and starts with dotnet Program.dll; a jar of the same program is 4 556 bytes and starts with java -jar. The first Maven build of a new project took 20.2 seconds because Maven downloaded its plugins; a C# build of a console project took about three seconds from a cold start and less after it, because the SDK is installed with the compiler.
Onboarding is the other side of the same coin. A newcomer to C# installs the SDK, runs dotnet new console and has a program that builds; a newcomer to Java installs a JDK, learns that javac and java are two commands, and meets Maven or Gradle on the first day of a real project. Both languages read alike to a programmer who knows either, which the pages of this group show by putting the two listings next to each other: the syntax is a week, and the platform is a month.
Maintainability
- The C# nullable analysis catches a dereference at compile time and warns; Java 21 has no such analysis and a helpful message at run time. For a large codebase the warning is worth more, because it arrives before the test and the deployment.
- Java's checked exceptions make a failure part of a signature, which a reader of a library sees at the call site; C# leaves the failure to the documentation and the tests. Neither is free: the first is verbose, the second is invisible.
- Properties and init keep the C# object small; getters, setters and builders keep the Java object explicit. A team that reads code more than it writes it may prefer either, and both have produced large maintainable systems.
- The tooling decides more than the syntax in the long run: a formatter, a linter and a test runner that ship with the SDK (dotnet format, the analyzers, dotnet test) are less to configure than a Java project's set of plugins, and a project that configures less is a project that drifts less.
The fair conclusion: choose the platform first, the team second and the language third. When the platform is Windows or Unity, that is C#. When it is Android, a JVM-only host or an enterprise stack already written in Java, that is Java. When the platform allows both, the difference this group measured is small enough that the team's experience and the library it needs should decide, and a language argument that rests on a benchmark of a loop should end with the benchmark page.
Exercises
- Take a program you maintain and list the platform constraints that decide its language.
- Find one library your project needs that exists in only one of the two ecosystems.
- Compare the onboarding of both in your team: what a new member installs and what they read on the first day.
- Write the case against your own preference, using a page of this group as evidence.
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
What should decide the language first, according to the page?
Answer
The platform the program must run on
The page puts the platform first, the team second and the language third.
-
Which platform is Java's large advantage?
Answer
Android
Android is Java and Kotlin, and no C# runtime is first class there.
-
Which area does the page name as C#'s advantage?
Answer
Value types and Span for a performance-sensitive program
Structs, Span and ref struct are the C# answer to allocation.
-
What did the throughput page measure?
Answer
122.6 ms against 129.8 ms medians, inside the noise
The page refuses to make a choice on that difference.
-
What does dotnet give a team that a Java project assembles?
Answer
The compiler, package manager, test runner and publisher in one tool
The page names the single toolchain as the C# advantage.
-
Which language warns about a null dereference at compile time?
Answer
C#, with nullable reference types
The null page shows the warning and the Java message at run time.
-
What is the cost of Java's checked exceptions?
Answer
Verbose signatures and callers that must decide
The page calls the trade verbosity against visibility.
-
What did the build page measure for a small C# publish?
Answer
128 KiB
The publish folder of the records program is 128 KiB.
-
When is C# the wrong choice?
Answer
When the target platform has no .NET and the team and libraries are Java
The page lists the platform, the team and the libraries as the reasons.
-
What does the page say about a language argument based on a benchmark?
Answer
It should end with the benchmark page, because the difference is inside the noise
The measured difference was five percent of a loop.
The C# program was built with the .NET SDK 10.0.303 and the Java program with OpenJDK 21.0.13.1 (javac 21.0.13.1), both run on Alpine Linux 3.24.3 (x86_64, four processors) in a Docker container.
Article author: Arthur Isaev