2
\$\begingroup\$

The following code will produce a run-time error, stackoverflow exception.

class Foo
{
    //static Foo foo = new Foo(); // you are not allowed using this approach
    //static readonly Foo foo = new Foo(); // you are not allowed using this approach
    Foo foo = new Foo();
}
class Program
{
    static void Main(string[] args)
    {
        new Foo();
    }
}

The objective is to cancel the exception without

  • removing, modifying or commenting out the existing code,
  • using the approaches mentioned (and their variants in access modifiers, public, private, etc) as the given comments,
  • using try-catch-finally or its variants,
  • using attributes,
  • using compilation condition,

You are only allowed to add your own code inside the Foo class. How do you cancel the exception at run-time?

\$\endgroup\$

closed as off-topic by Timtech, Martin Ender, Peter Taylor, NinjaBearMonkey, ProgramFOX Dec 1 '14 at 16:53

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions without an objective primary winning criterion are off-topic, as they make it impossible to indisputably decide which entry should win." – Timtech, Martin Ender, Peter Taylor, NinjaBearMonkey, ProgramFOX
If this question can be reworded to fit the rules in the help center, please edit the question.

  • \$\begingroup\$ It would be best to tag this question as either popularity-contest or code-golf so it's clear how you will objectively declare a winner. \$\endgroup\$ – Darren Stone Jan 6 '14 at 1:51
  • \$\begingroup\$ I probably will add more rules if your ideas are too trivial. Sorry for this inconvenience. \$\endgroup\$ – kiss my armpit Jan 6 '14 at 2:02
  • 5
    \$\begingroup\$ Pulling the rug out like that may be considered rude. It's best to think your question through thoroughly before asking the internet to spend effort on it. :-) \$\endgroup\$ – Darren Stone Jan 6 '14 at 2:10
  • 1
    \$\begingroup\$ This is a popularity-contest yet the accepted answer has few votes. You should accept the highest-voted answer (see popularity-contest) \$\endgroup\$ – Justin Jan 10 '14 at 3:43
  • 1
    \$\begingroup\$ @Quincunx: I changed the tag. \$\endgroup\$ – kiss my armpit Jan 10 '14 at 3:58

10 Answers 10

9
\$\begingroup\$

Never wrote C# before, does this work ?

class Foo
{
    Foo fooExit = exitMe();
    //static Foo foo = new Foo(); // you are not allowed using this approach
    //static readonly Foo foo = new Foo(); // you are not allowed using this approach
    Foo foo = new Foo();

    static Foo exitMe()
    {
        System.Environment.Exit(0);
        return null;
    }
}
class Program
{
    static void Main(string[] args)
    {
        new Foo();
    }
}
\$\endgroup\$
  • \$\begingroup\$ Well, the rules forbid "using attributes". \$\endgroup\$ – Victor Stafusa Jan 6 '14 at 2:24
  • \$\begingroup\$ @Victor: The code above does not contain any attributes. \$\endgroup\$ – kiss my armpit Jan 6 '14 at 2:25
  • \$\begingroup\$ @StiffJokes Interesting. I am from Java and never programmed in C#. In Java the Foo fooExit = exitMe(); would be a declaration of an attribute called fooExit. I.E. For me attribute and field were synonyms. Googling this, it looks like that in C# attributes are more-or-less what in java are called annotations. Thank you, did not knew this! \$\endgroup\$ – Victor Stafusa Jan 6 '14 at 2:32
  • \$\begingroup\$ I changed the tag just for attracting more creative method. :-) \$\endgroup\$ – kiss my armpit Jan 6 '14 at 2:36
  • \$\begingroup\$ The winner might be different from the accepted answer. It does not matter for me. \$\endgroup\$ – kiss my armpit Jan 6 '14 at 13:06
31
\$\begingroup\$

Here, another try:

class Foo
{
    //static Foo foo = new Foo(); // you are not allowed using this approach
    //static readonly Foo foo = new Foo(); // you are not allowed using this approach
    Foo foo = new Foo();
    MWAHAHAHA, THIS LINE GIVES A COMPILE ERROR! NO STACKOVERFLOW EXCEPTION ANYMORE! LOL
}
class Program
{
    static void Main(string[] args)
    {
        new Foo();
    }
}
\$\endgroup\$
  • 1
    \$\begingroup\$ It is really funny, really. \$\endgroup\$ – kiss my armpit Jan 6 '14 at 2:20
  • 3
    \$\begingroup\$ That's what I was gonna do... \$\endgroup\$ – boothby Jan 6 '14 at 2:43
25
\$\begingroup\$

How about this?

class Foo
{
    //static Foo foo = new Foo(); // you are not allowed using this approach
    //static readonly Foo foo = new Foo(); // you are not allowed using this approach
    String str = @"
    Foo foo = new Foo();
    ";
}
class Program
{
    static void Main(string[] args)
    {
        new Foo();
    }
}
\$\endgroup\$
14
\$\begingroup\$
class Foo
{
    public class Bar
    {
        Foo foo = new Foo();
    }
}
class Program
{
    static void Main(string[] args)
    {
        new Foo();
    }
}

I don't have a C# compiler at hand, but I suspect this might work. The idea is to put the definition in a nested class, such that foo would only be assigned if an instance of Bar is created. Now, the problem is that (I believe) the nested class needs to be public. I don't know if this is a rule violation, i.e. that the rule applies to any use of an access modifier, or only if applied to foo. (Could someone who can compile C# please try this with and without the public?)

\$\endgroup\$
  • 2
    \$\begingroup\$ Works with and without public. You also could've tested this on an online compiler, e.g. ideone. \$\endgroup\$ – Bob Jan 6 '14 at 6:04
8
\$\begingroup\$
class Foo
{
    Foo(bool recursion = false)
    {
        if (recursion)
            Foo foo = new Foo();
    }
}
class Program
{
    static void Main(string[] args)
    {
        new Foo();
    }
}
\$\endgroup\$
8
\$\begingroup\$

To simplify Florent's code:

class Foo
{
    Foo foo = new Foo();

    static Foo() {
        System.Environment.Exit(1);
    }

}
\$\endgroup\$
  • \$\begingroup\$ Does this actually work? Won't Foo foo = new Foo(); be (recursively) evaluated before any constructor is executed? \$\endgroup\$ – nitro2k01 Feb 18 '14 at 9:35
  • \$\begingroup\$ @nitro2k01 No, it won't. The static ctor is executed before any instance member initialization. \$\endgroup\$ – Andris Feb 18 '14 at 10:07
3
\$\begingroup\$
class Foo
{
    while (true);
    Foo foo = new Foo();
}
class Program
{
    static void Main(string[] args)
    {
        new Foo();
    }
}

Its not a bug, it's a feature!

In case that doesn't work

class Foo
    if(false)
        Foo foo = new Foo();
}

class Program
{

    static void Main(string[] args)
    {
        new Foo();
    }
}
\$\endgroup\$
  • 1
    \$\begingroup\$ I guess that stopping stack overflow from happening by making infinite loop prior to it counts. It doesn't appear to be disallowed. \$\endgroup\$ – Konrad Borowski Jan 10 '14 at 13:27
  • \$\begingroup\$ @xfix Indeed. Since the problematic code will never be executed, it cannot cause a lockup. Aka: You cannot lock me up because I'll lock me up first :-P \$\endgroup\$ – Mark Jan 10 '14 at 13:30
  • 2
    \$\begingroup\$ I can't see how that would compile in C#. I'm getting a Invalid token 'while' in class, struct, or interface member declaration compiler error with your code. \$\endgroup\$ – Andris Jan 10 '14 at 13:39
  • 1
    \$\begingroup\$ @Mark - Hmmm, seems a little passive-aggressive... ;) I like it. \$\endgroup\$ – simon Jan 13 '14 at 22:38
  • \$\begingroup\$ @simon It's called c-SHARP, because its not for the soft-hearted :-P \$\endgroup\$ – Mark Jan 14 '14 at 6:39
3
\$\begingroup\$

The Answer is:

class Foo
{
    //static Foo foo = new Foo(); // you are not allowed using this approach
    //static readonly Foo foo = new Foo(); // you are not allowed using this approach

    public void Method()
    {
    Foo foo = new Foo();
    }
}
class Program
{
    static void Main(string[] args)
    {
        new Foo();
        Console.ReadLine();
    }
}
\$\endgroup\$
1
\$\begingroup\$

Note: The rules changed since I posted this.

That is it:

class Foo
{
    //static Foo foo = new Foo(); // you are not allowed using this approach
    //static readonly Foo foo = new Foo(); // you are not allowed using this approach
    Foo foo = null; // Removing code != modifying.
}
class Program
{
    static void Main(string[] args)
    {
        new Foo();
    }
}
\$\endgroup\$
  • \$\begingroup\$ It is almost funny, just almost. :-) \$\endgroup\$ – kiss my armpit Jan 6 '14 at 2:13
  • \$\begingroup\$ Your trivial code can actually be simplified as Foo foo; to make it almost funnier. \$\endgroup\$ – kiss my armpit Jan 6 '14 at 2:22
  • \$\begingroup\$ @StiffJokes In this case, you could argue that I was deleting code, against the rules (as they were at that time). \$\endgroup\$ – Victor Stafusa Jan 6 '14 at 2:23
0
\$\begingroup\$
class Foo
{
    //static Foo foo = new Foo(); // you are not allowed using this approach
    //static readonly Foo foo = new Foo(); // you are not allowed using this approach
    Foo foo = true?null: new Foo();
}
class Program
{
    static void Main(string[] args)
    {
        new Foo();
    }
}
\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged or ask your own question.