aGuy
aGuy

Reputation: 217

How to produce a StackOverflowException with a few lines of code?

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

Upvotes: 8

Views: 1386

Answers (8)

Sebastian Piu
Sebastian Piu

Reputation: 8008

public static void Main()
{
  Main();
}

Upvotes: 5

nan
nan

Reputation: 20296

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");
}

Upvotes: 18

user467871
user467871

Reputation:

Run this code (recursion):

f () {
       f();
    }

Upvotes: 1

GvS
GvS

Reputation: 52518

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

private int _num;
public int Num {
   get { return Num; }
   set { _num = value; }
}

Upvotes: 4

Femaref
Femaref

Reputation: 61427

public int Method(int i)
{
  return i + Method(i + 1);
}

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

Upvotes: 2

Robert
Robert

Reputation: 6540

in pseudocode

func(): call func()

Upvotes: 3

SLaks
SLaks

Reputation: 887215

Like this:

A() { new A(); }

Upvotes: 22

BoltClock
BoltClock

Reputation: 723388

throw new StackOverflowException();

Cheating, I know... :)

Upvotes: 41

Related Questions