How to create a StackOverflowException with multiple lines of code?

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

+8
c # stack-overflow
source share
8 answers

Like this:

 A() { new A(); } 
+22
source share
 throw new StackOverflowException(); 

Cheating, I know ... :)

+41
source share

Not the shortest, but the funniest :)

 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"); } 
+18
source share
 public static void Main() { Main(); } 
+5
source share

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

 private int _num; public int Num { get { return Num; } set { _num = value; } } 
+4
source share

in pseudo code

 func(): call func() 
+3
source share
 public int Method(int i) { return i + Method(i + 1); } 

I think this should work. In general, any recursion that does not end there.

+2
source share

Run this code (recursion):

 f () { f(); } 
+1
source share

Source: https://habr.com/ru/post/650245/


All Articles