C# Quines

The following programs (which should each be written all on one line) are C# quines. ‘Quines’ are programs that construct and output copies of their own code (without using tricks such as reading their own source files, reflection, or custom external classes). The Recursion Theorem shows that a quine exists in any universal programming language that can output any of characters that can occur in its syntax. The quines below were written by Larry Croney and are used with his permission.

class c{static void Main(){string s="class c{{static void Main(){{string s={0}
{10};System.Console.Write(s,(char)34,s);}}}}";System.Console.Write(s,(char)34,
s);}}
class c{static void Main(){s+=(char)34;System.Console.Write(s+s+';'+'}');}stat
ic string s="class c{static void Main(){s+=(char)34;System.Console.Write(s+s+'
;'+'}');}static string s=";}
class c{static void q(string s){s+=(char)34;System.Console.Write(s+s+')'+';'+'
}'+'}');}static void Main(){q("class c{static void q(string s){s+=(char)34;Sys
tem.Console.Write(s+s+')'+';'+'}'+'}');}static void Main(){q(");}}

Challenges

  • Construct a shorter quine (the shortest quine above is 161 characters).
  • Construct a quine that doesn’t even use the internal classes (except Object, from which all classes inherit). This means it can’t use either Strings or Arrays, and will instead have to encode the data across a number of longs (but remember you can’t use an array of longs) and decode it to chars.
This article was last edited on 15th April 2007. The author can be contacted using the form below.
Back to home page
Bookmark with: