Piano keys

The Good and the Bad of C# Programming

A programming language is considered topnotch when it's easy to learn, performs well, and can be used for any type of task, application, or platform. Today, we’re looking at C#, a standard representative among other .NET technologies. For decades, C# has been a preferred language for backend architecture and Windows apps. And for a reason.

Looking at the peculiarities of С# programming, we’ll discuss specific features and problems existing, and figure out how it compares to other languages like Java. Analyzing both strong and weak sides of C#, we’ll understand if it’s worth the candle.

What is C# programming: a short history of development and how it works today

C# (pronounced as See Sharp) is a general purpose, object-oriented programming language designed by Microsoft. The name is inspired by the sharp symbol, used in musical notation to raise a note by a semitone. If you look closely, a # sign consists of four pluses placed in a two-by-two grid. This indicates that C# is an increment of C++. Released in 2000, C# remains the major technology for creating desktop applications on Windows.

Applications written on C# use .NET runtime, class libraries, and basically the actual .NET framework, so both technologies are often seen as inseparable. Presently, .NET is a platform and a programming framework for cross-platform development.

You can find more information by clicking on the following links:

C# documentation — official Microsoft guidelines and materials

.NET developer community — a Microsoft community for C# and .NET developers

C# forum — a popular third-party resource

C# 101 video series — The basics of C# programming

The first iteration of C# 1.0 shipped with Visual Studio and .NET in 2002 was similar to Java in many ways, which is also stated in the documentation. “When you go back and look, C# version 1.0, released with Visual Studio .NET 2002, looked a lot like Java. At the time, looking like Java meant it achieved those early design goals.” At the same time, C# lacked such functionality as asynchronous programming, multithreading support, and exception filtering to be comparable with Java directly. C# along with .NET remained a Windows-only technology.

You can watch our video to learn more about .NET, as it is tightly connected with C#.

The Pros and Cons of .NET FrameworkPlayButton

The Pros and Cons of .NET Framework

With the acquisition of Xamarin and its native Mono Runtime in 2016, .NET and C# got cross-platform capabilities for the first time. Because of this, C# applications can be coded once and launched on any platform, including Web apps, Windows, macOS, Linux, Android, and iOS.

C# ecosystem

That said, C# 9.0 is the latest version of the language used together with .NET 5, and .NET Core. So, let’s talk about the C# ecosystem that makes it a go-to solution for general programming purposes.

.NET Platform. C# is inseparable from the .NET platform. It compiles and runs programs via a

Common Language Runtime (CLR). The latest version of the .NET platform is .NET 5.

.NET Core framework. Core used to be a cross-platform version of the old .NET. It provides libraries and reusable elements to speed up application development. Since .NET 5 already has all cross-platform features of Core, .NET 5 may be considered the single new .NET product.

Visual Studio. It’s a native IDE for C# that allows developers to download and install the language, write code, debug, and run/compile it.

.NET SDK (read as C# SDK). The set of tools and libraries is created to develop C# applications or write new libraries.

С# compilation and execution

C# is a compiled language. To run an application, normally a developer compiles C# source code into intermediate language (IL). This way, it can be executed across different target systems. During the execution, IL code is compiled further into machine code of the current target system and CPU executes it on the go.

С# compilation and execution

This is a standard scenario and there may be variations depending on the target platform and type of an application

Just-in-time (JIT) compilation from IL enables versatility of C# applications that can be run everywhere, from Windows to PlayStation. But it also introduces some performance overhead compared to, say, C++ which is compiled into the target system’s machine code right away, before being executed. But don’t despair, runtimes used with C# support a number of cases when IL is compiled ahead of execution to enable the best performance possible. For example, iOS apps all require ahead-of-time compilation.

There are two main runtimes for C#.

.NET Common Language Runtime  — used to run and compile desktop and web applications with .NET 5 and Core versions. It supports Windows, Linux, MacOS, Android, iOS -- basically all major target platforms.

Mono Runtime — an addition that originally came with Xamarin to work on mobile apps (iOS/Android), Linux, and macOS. Today, Mono is used for applications that have critical performance requirements, like games or mobile apps. It also supports all major operating systems, with the addition of some gaming consoles.

Now, let’s look at the pros and cons of C# programming.

The pros of C# programming

C# is considered a great choice for Windows desktop apps, enterprise solutions, and even game development, as Unity game engine is built on C#. So what makes C# advantageous as your main programming language?

Object-oriented programming

From the start, C# was based on the object-oriented programming (OOP) principles. This coding concept assumes you can define the type and structure of data, to apply the set of standard functions to it. OOP gathers data into objects, which makes it easier to break the application into smaller pieces that are faster to build, manage, and combine.

Using OOP, objects can be managed without interacting with their inner attributes, describing object behavior through declaration of classes. OOP languages produce applications that are easier to test and read, enable respose to any occurring issues, and generally mean a leaner approach to writing code.

High-level language with memory access capabilities

C# language is considered a high-level language because its syntax resembles human language. In other words, it has a high level of abstraction from a machine code, which is why we need to compile the code written in C# for the hardware to understand its commands.

High-level languages are beneficial for developers because they have a simpler syntax to understand and manage, as opposed to low-level languages like C.

C# hello world example

An example of how Hello World looks like in C#. Source: docs.microsoft.com

But this point is a bit controversial. One of the important features languages like C have is that they can access memory directly, using specific types of commands called pointers. While C# is a much higher level language, a limited range of pointer features is still available for developers.

Designed as a part of .NET platform: cross-platform runtimes, and extensive resources of .NET frameworks

C# on its own would probably suggest a narrow focus on the desktop application for Windows only. With the extensive capabilities of .NET, it becomes a flexible language that can be used cross-platform.

.NET supports C# developers with different types of runtime environments such as the aforementioned Mono and Microsoft’s own CLI. A runtime is used in .NET applications to translate C# code into machine instructions on any of the supported OS.

Language interoperability is another feature of .NET that makes C# programming convenient. Interoperability means that C# code can interact with applications written in compatible languages such as C++, F#, Visual Basic, and Windows PowerShell. This allows developers to use different languages in a single assembly.

With .NET, C# developers can also rely on the powerfull .NET Core framework designed for building web services and applications. The framework comes with a set of customizable components, unified class libraries, and other features that significantly speed up the development process.

C-family of languages

C# relates to the C-family of programming languages. While it’s not beneficial on its own, learning C# will make it easier to learn C, C++, or Java, as these languages utilize similar syntax and programming principles.

For business, it's also a pro, since developers can support each other on C-related projects. Namely, the interoperability feature in C# makes it possible to merge technology stacks in one project.

Visual Studio as a native IDE

An Integrated Development Environment (IDE) is a workspace for programmers that provides the means for writing, testing, debugging, and compiling code during development.

Several features of Visual Studio make it a reliable option as an inbuilt IDE:

  • a single workspace for all .NET applications;
  • support for other languages as Visual Basic, C, C++, and JSON;
  • an integrated compiler, debugger, and garbage collector;
  • customizable environment and convenient UI; and
  • inbuilt collaboration via GIT, which means your team can work on a single code base and collaborate for code-reviews.

Visual Studio is considered one of the industry standards of how an IDE should look. It is used for all types of .NET applications written in C# or any other supported language. The use of other C-related languages and their frameworks is also available in Visual Studio.

Inbuilt garbage collector

Managing the memory allocation is one of the crucial tasks for maintaining application performance. For this purpose, C# has an inbuilt garbage collector.

A garbage collector is a memory manager that tracks the unused objects and frees memory automatically. When working with the managed code, it usually requires developers to write additional code to avoid memory leaks. Automatic memory management frees up developers from writing commands to reclaim the unused objects, cleanse the memory, and allocate it to the new ones.

Type-safe language with dynamic capabilities

C# is type-safe which means that a variable can’t change its type across the code. For example, if you’ve declared a variable GoodDay as integer, you can only assign exact numeric values to it, and string values like Saturday or Sunday are unacceptable.

Type safety ensures that the variable will behave in a predictable manner and any operations on it will be possible only if they match the type. No surprises! The code containing the GoodDay variable will access only those memory slots that are meant for integers. This approach makes the source code generally less error-prone.

C# by default performs type checking at the compile time, called static typing. As a result, type errors are pinpointed as early as possible, before leaking into the execution environment. However, starting from version 4, the language also supports dynamic typing. You can create objects marked as dynamic that allows them to bypass type checking at compile time. The error, if present, will be caught at runtime.

The dynamic option gives developers more flexibility and simplifies interactions with pieces of code coming from other runtimes. For instance, your object can get its value from a dynamic language such as IronPython.

Thorough documentation

C# is actively developed and maintained by Microsoft. Within the last 4 years, there were 3 major releases along with .NET 5, which significantly improved development experience.

Microsoft provides extensive documentation for C# and .NET, including interactive tutorials, video series, and problem explanation. A large part of documentation is devoted to the .NET resources like the common type system, compilers, and asynchronous programming capabilities with C#. The rest of pages covers the basics of C# programming on different versions of the language.

Large community

A C# developer is often referred to as .NET developer, since the stack of technologies used for programming is similar in most cases. C# is in 8th place among the most popular programming languages according to the Stackoverflow Developer Survey 2021 among professional developers.

C# and .NET developer survey

C# and .NET positions in Stackoverflow Developer Survey. Source: stackoverflow.com

Combined with .NET Core occupying 5th place, there is a large community of programmers who use C#. This guarantees you can always find support or collaboration opportunities in the C# community.

Besides the .NET and C# forum, there is also an official resource called .NET foundation, a place for collaboration and sharing ideas.

All of these features make C# a good choice for enterprise solutions or large production teams. Although, there are several cons you need to consider in terms of language specifics.

The cons of C# programming

There is always a tradeoff between the programming language features. Since what’s beneficial in one case, can be a challenge in another.

C# performance isn’t the absolute best

The performance of a language can be measured in terms of compilation time and the actual application performance. Compared to its closest counterpart Java, C# has similar compilation time. But the application performance benchmarks show slightly faster results for C# .NET .

When it comes to C++ vs C# benchmarks, the former wins. But keep in mind, there is a slight difference between how C++ and C# code is compiled. Normally, C# passes two phases of compilation: The first is when the code compiles into intermediate language at the build stage, and the second is the compilation into a machine code when it’s executed at a runtime. Considering the benchmarks, there is a significant difference between the languages with C++ outpacing C#.

On the bright side, the latter is quicker to compile at the build stage.

Performance benchmark of C# vs C++. Source: benchmarksgame-team.pages.debian.net

Of course, the subject of performance depends on many factors such as code optimization and used runtime. And while there is no clear winner between Java and C#, C++ jumps ahead of both of them in terms of code execution time.

Dependence on .NET platform

As we mentioned, C# heavily relies on .NET resources to run on different operating systems or platforms. However, on its own it's not that flexible, if you’re not considering .NET as your main technology stack.

This is a question of comparison. Namely, a compiled Java program can run on any platform where Java Virtual Machine is available. With C#, you need to use different runtimes for different platforms and adapt the code for the corresponding system requirements. While .NET provides all the resources for it, bare C# code can’t be used that easily.

Hard learning curve

While C# is not the easiest language to learn on its own, using .NET libraries adds another layer of complexity. Libraries in .NET are often updated and there are thousands of resources you need to learn before getting your job in C# programming. Not to mention you’ll have to work with multiple runtimes if you want to develop cross-platform applications as C# or .NET apps don't work on Linux or macOS natively.

The presence of low-level functions such as pointer arrays (that provide direct access to memory) can be also a con in terms of learning the language. While C# is a type-safe language, pointer functions are omitted by the runtime check. Which can lead to low-performance or breaks.

Nevertheless, all of these issues can be addressed through reading Microsoft documentation. Which shouldn’t prevent you from choosing C# as your main language. So now, let’s look at some actual use cases of C#.

Where C# is used: common applications

Now, that we’ve discussed key advantages and pitfalls, one last question to be answered is: When does it make perfect sense to opt for C#? Though the language is referred to as general-purpose, there are a few areas where it’s most welcome.

Windows desktop applications. C# is a standard choice for Windows applications because of the native support for .NET frameworks. It provides tons of libraries, components, UI class libraries and other resources that speed the development.

Web services and apps. With the help of .NET Core, it is also possible to develop reliable web services using the same resources of the .NET platform.

Linux and macOS applications. Using Mono runtime, the same applications can be optimized for Unix-based systems and macOS devices. In many cases, Mono shows performance results comparable to .NET runtime. Which means, there is no significant difference in how C# applications work on different platforms.

Mobile development becomes possible with Xamarin that uses C#. Xamarin is a cross-platform framework that wraps native components and libraries into the .Net layer to create native-looking Android and iOS apps. It enables developers to reuse up to 90 percent of the code across two major mobile platforms.

Game development. As we already mentioned, C# is a go-to language for game development, since it’s used in the Unity game engine. The games can be built for various platforms such as XBox, PlayStation, Nintendo devices, mobile games for Android and iOS, and PC games (Win, Mac, Linux).

This post is a part of our “The Good and the Bad” series. For more information about the pros and cons of the most popular technologies, see the other articles from the series:

The Good and The Bad of Xamarin Mobile Development

The Good and the Bad of JavaScript Full Stack Development

The Good and the Bad of ReactJS and React Native

The Good and the Bad of Swift Programming Language

The Good and the Bad of .NET Framework Programming

The Good and the Bad of Angular Development

The Good and the Bad of Selenium Test Automation Tool

The Good and the Bad of Android App Development

The Good and the Bad of Java Programming

Comments