The shortest quine in C# 9 and 10

Development General .NET Core

2 years ago

This article is part of the C# Advent, annual blogging event hosted by Matthew D. Groves. Thanks for organizing it! Have you ever heard the term quine in programming? If you did not, don't worry, it is not something that will come up in your next programming interview. Here is the definition:

A quine is a computer program which takes no input and produces a copy of its own source code as its only output.

Someone scanning a PC screen

A real-world equivalent of a quine

Essentially you can imagine a quine as a source code, which, during its execution, will output its own source code and terminate. It is a fascinating concept and there is a lot of fun facts related to it, including mind-twisting variants like quine-relays (programs producing programs writing the original program), multiquines (set of multiple programs each able to produce any other of the programs based on input argument) and radiation-hardened quines (a program which produces the original code even if any of its characters is removed) - which are just fascinating and worth checking out on Wikipedia. For our purposes however, we will stick with a simple quine and we will try to see how the latest release of C# allows us to make it much shorter.

The canonical C# quine

Here is the most commonly used example of a C# quine:

As it is hardly readable this way, here is the same thing with whitespace and indentation added for clarity:

When executed, this code will output itself. As it may be a bit confusing to understand at first, let's break it down. The code starts as any normal C# program - with a class declaration and a static void Main() method. The statement on line 5 is declaring a string variable which will act as the main "source" for the program's output. This string starts up a copy of the source code itself - so again class, Main() up until the point of the variable declaration - this is where things start to get interesting. We see there {0}{1}{0} which reveals the string is actually a format string with placeholders {0} and {1}. Their purpose is revealed on line 6. Following the placeholders in the string is a literal copy of the content of lines 6, 7 and 8. Line 6 provides the actual output of the program using System.Console.Write. This method can work with format strings, so we pass in our string variable, then a cryptic (char)34 and then the variable itself again. Doing a cast of a integer to char gives you a character of that value - in this case ASCII 34, which is our favorite double-quote character ". This character is then plugged in the string in place of {0} placeholder, and {1} is replaced by the string itself - together producing the value assignment you can see on line 5. It takes a bit to wrap head around this, but in the end it makes sense. The trick with (char)34 especially is super neat, as you can easily avoid having to find some complicated ways to escape the double-quote character within the string. In total, this C# quine is 157 characters long. But thanks to C# 9 and C# 10 we can make this quine much shorter!

C# 9: Top-level statements

C# 9 allows us to write single-file programs with just top-level statements, without having to wrap them in a class and Main() declaration. So let's do that:

By eliminating the class and method declaration and all curly braces, we are able to cut the length down to just 95 characters.

C# 10: Implicit usings

It is quite clear, that the code is already quite short and there is not much space to trim further, but C# 10 allows us to drop several more characters thanks to implicit usings. This is what Visual Studio 2022 generates for the default Console app project template:

Implicit usings operate based on the target .NET SDK. For Microsoft.NET.Sdk they generate a file called Project.GlobalUsings.g.cs which contains the following:

Thanks to this, the System namespace, which contains Console class is automatically included and we no longer have to prefix Console usage with it. Hence, our quine in C# becomes:

Which is just 81 characters!

Can it get shorter? Yes!

I believe that this 81 character quine is the shortest currently possible in C# 10. It is not possible to compile an empty file (equivalent of an empty Main method) and any non-empty code must definitely use Console.Write to output some text, and at least the Console.Write statement itself must be contained within it - which inevitably leads to some form of declaration of a string variable, which brings us back to our final solution. I was thinking of a program that could read and output its source file's contents, but that is a cheat-quine and does not pass the rules. But it is also possible that I am wrong and there is a way to shave of a few more characters so if you find another solution or shortcut please post it in the comments or on Twitter (the character limit will not be a problem in this case ?). Jonathan Peppers was the first to point out, that the quine can get even shorter, if we further utilize global static using on the Console class. I originally thought of this too, but it felt like cheating if it were a using in another C# file. However, as Jonathan explained (and what I had no clue about) - these usings can also be specified directly in the csproj file, which definitely feels much less "against the rules"! So, we can update the .csproj as follows:

And now our quine becomes:

Which is now down to just 65 characters! Thanks Jonathan!