Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
No, this post is not about viruses. The problem is to write a C# program that prints to the console an identical copy of its own source code.
If you ever went to this exercise in other languages, you already know that the a self-replicating program in C looks very different than the Pascal version is different than the Prolog version, and so on... And, surprisingly, C# has some new syntactical constructs that allows us to take a completely different approach!
I posted a solution in the “comments“ area for this post. Is probably not the shortest one but I found it pretty concise. And I'm sure you will find better solutions... :-)
Comments
- Anonymous
July 07, 2004
class C
{
const string s = @"
class C
{{
const string s = @{1}{0}{1};
static void Main()
{{
System.Console.WriteLine(s,s,'u0022');
}}
}}";
static void Main()
{
System.Console.WriteLine(s,s,'u0022');
}
} - Anonymous
July 07, 2004
The techincal term for this is a "quine." - Anonymous
July 07, 2004
In my previous comment, the leading spaces got lost. I just replaced them with dots, hopefully this will enhance the clarity of the sample...
------------------------
class C
{
....const string s = @"
class C
{{
....const string s = @{1}{0}{1};
....static void Main()
....{{
........System.Console.WriteLine(s,s,'u0022');
....}}
}}";
....static void Main()
....{
........System.Console.WriteLine(s,s,'u0022');
....}
} - Anonymous
July 07, 2004
The comment has been removed - Anonymous
July 07, 2004
I did this a while ago you can see my solution at <a href="http://weblogs.asp.net/whaggard/articles/3638.aspx">http://weblogs.asp.net/whaggard/articles/3638.aspx</a>
Wes - Anonymous
July 07, 2004
Sorry the link got busted try this:
http://weblogs.asp.net/whaggard/articles/3638.aspx