8

How can I produce a StackOverflowException with minimal lines of code?

BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
aGuy
  • 217
  • 1
  • 3
  • 8
  • Duplicate: http://stackoverflow.com/questions/62188/stack-overflow-code-golf –  Dec 17 '10 at 16:15

8 Answers8

41
throw new StackOverflowException();

Cheating, I know... :)

BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
  • this is the simplest way to generate any kind of exceptions for testing – Kinjal Dixit Dec 17 '10 at 16:11
  • Jajaja... i was thinking in something "a little more elaborated", but it works too.. – aGuy Dec 17 '10 at 16:13
  • @kinjal For this specific exception it's going to be hard to test - you cannot catch a StackOverflowException. – Tim Lloyd Dec 17 '10 at 16:29
  • 3
    @chibacity: `try { throw new StackOverflowException(); } catch (StackOverflowException ex) { Console.WriteLine("Yes you can."); }` (sorry, I know it's getting old :) – BoltClock Dec 17 '10 at 16:30
  • @BoltClock Well I'll be jiggered. You are quite correct. It must be only when it is generated via the OS and it is treated as an exception of doom i.e. an application flattener. – Tim Lloyd Dec 17 '10 at 16:51
  • @chibacity: Of course, it'll be an inevitable crash at that point. – BoltClock Dec 17 '10 at 16:56
  • @BoltClock Well to be precise, the fact that it is treated as fatal is a design choice in the .Net framework. In .Net 1.0 you could catch and ignore a StackOverflowException. – Tim Lloyd Dec 17 '10 at 16:58
22

Like this:

A() { new A(); }
SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • One line, but fewer characters and produces a *real* exception... +1 (does it not need a `void` return in C# though?) – BoltClock Dec 17 '10 at 16:12
  • @BoltClock: It's a constructor. – SLaks Dec 17 '10 at 16:13
  • Ah OK. I see you just added the `new`. – BoltClock Dec 17 '10 at 16:15
  • agree, can you explain a little whats happening here, im new to .net and all this jargons. – aGuy Dec 17 '10 at 16:15
  • @aGuy: It's a constructor that calls itself. A slightly longer, but clearer, version would be `int A() { return A(); }`. – SLaks Dec 17 '10 at 16:16
  • @aGuy: Basically you have a class with a constructor that makes a new object of the same class. So essentially it's a constructor calling itself recursively. – BoltClock Dec 17 '10 at 16:17
  • In Java, you can call another (or presumably the same) constructor with a `this` call. So you get `A() { this(); }` which reduces a character. Is this doable in C#? – corsiKa Nov 18 '11 at 14:13
18

Not the shortest one but funny:)

public static bool IsNotEmpty(string value)
{
    return !IsEmpty(value);
}

public static bool IsEmpty(string value)
{
    return !IsNotEmpty(value);
}

public static void Main()
{
    bool empty = IsEmpty("Hello World");
}
BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
nan
  • 19,595
  • 7
  • 48
  • 80
5
public static void Main()
{
  Main();
}
BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
Sebastian Piu
  • 7,838
  • 1
  • 32
  • 50
4

I always use this code (because it is harder to detect) :-(

private int _num;
public int Num {
   get { return Num; }
   set { _num = value; }
}
GvS
  • 52,015
  • 16
  • 101
  • 139
  • 1
    It would be even better if the field name was `num` (without the underscore prefix). ;) – ShdNx Nov 27 '11 at 22:00
3

in pseudocode

func(): call func()
Robert
  • 6,412
  • 3
  • 24
  • 26
2
public int Method(int i)
{
  return i + Method(i + 1);
}

I think this should work. In general, any recursion that doesn't terminate.

Femaref
  • 60,705
  • 7
  • 138
  • 176
1

Run this code (recursion):

f () {
       f();
    }