My C# Quine

by joey.westcott 20. January 2008 10:51

I was just dorking it up on the net today and I found an old topic that I haven't thought about in a long time. 

The Quine: (wikipedia)
In short it is a program in which the output or result of the code is the output of the program itself.  So it really just is a program that's only goal is to output the source code of the program.  Many find it "fun" to see how short of a program they can create, for this reason most will leave out formatting including return/line breaks or anything else that is not required for the program to function.

After a little search on the topic I ran into this website that had an example of quines for a ton of different programming languages, but I didn't see any for C# or for that matter none for any .Net languages.  This is probably because the site is old and has not been updated in a long time.  So as I started to type in google "C# quine" I stopped myself and decided that I would just write my own... so I did.  As you can tell I didn't aim for the shortest possible quine, but this being my first run at this I just decided to keep it simple. Maybe I will redo this making the shortest one that I can.

First I'll show you the program with all the nice formatting that you are used to with Visual Studio, I have removed the formatting because I didn't want to include it in the output of the program.  I did include it at first and it was working just fine but it adds a lot of more junk that is just really not needed.

Code after the break


   1:  using System; 
   2:  class Q 
   3:  { 
   4:      static void Main() 
   5:      { 
   6:          string s = "using System;class Q{2}static void Main(){2}string s ={1}{0}{1};Console.Write(string.Format(s, s, (char)34, (char)123, (char)125));{3}{3}";
   7:          Console.Write(string.Format(s, s, (char)34, (char)123, (char)125)); 
   8:      } 
   9:  } 



And now my real Quine in all its one line glory:
(fyi its 252 characters long, the shorter ones are just under 200 in C#, so you can see I still could trim some fat on this one.)
   1:  using System;class Q{static void Main(){string s ="using System;class Q{2}static void Main(){2}string s ={1}{0}{1};Console.Write(string.Format(s,s,(char)34,(char)123,(char)125));{3}{3}";Console.Write(string.Format(s,s,(char)34,(char)123,(char)125));}} 


Update: 01/21/2008
Ok here is my short version of the one above. Not bad; only 166 characters long. I'm not sure if this is one of the shorter ones around, I will have to search and see if I can find one shorter. 

   1:  class Q{static void Main(){string s ="class Q{{static void Main(){{string s ={1}{0}{1};System.Console.Write(s,s,(char)34);}}}}";System.Console.Write(s,s,(char)34);}} 


Update: 11/11/2008
Igor Ostrovsky wrote a nice article on quines, check it out here. Also here is the updated quine, now at 149 characters!

   1: class Q{static void Main(){var s="class Q{{static void Main(){{var s={1}{0}{1};System.Console.Write(s,s,'{1}');}}}}";System.Console.Write(s,s,'"');}}

Currently rated 4.7 by 3 people

  • Currently 4.666667/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

Development

Comments

Powered by BlogEngine.NET 1.4.5.0
Theme by Mads Kristensen