The exception of type "System.StackOverflowException" is fixed

Why is this? This is my code:

public class KPage { public KPage() { this.Titolo = "example"; } public string Titolo { get { return Titolo; } set { Titolo = value; } } } 

I set the data constructor. So, I would like to do something like

 KPage page = new KPage(); Response.Write(page.Titolo); 

but I get this error:

 set { Titolo = value; } 
+7
source share
3 answers

Here you have an infinite loop:

 public string Titolo { get { return Titolo; } set { Titolo = value; } } 

The moment you reference Titolo in your code, a getter or setter calls a receiver that calls a getter that calls a getter that calls a getter that calls a getter ... Bam - StackOverflowException .

Use a support field or use automatically implemented properties :

 public string Titolo { get; set; } 

Or:

 private string titolo; public string Titolo { get { return titolo; } set { titolo = value; } } 
+36
source

You have a self-regulator. You probably used automatic properties:

 public string Titolo { get; set; } 
+3
source

Change to

 public class KPage { public KPage() { this.Titolo = "example"; } public string Titolo { get; set; } } 
+2
source

All Articles