An unhandled exception of type 'System.StackOverflowException' occurred

前端 未结 3 569
广开言路
广开言路 2020-12-18 00:54

Why this? This is my code :

public class KPage
{
    public KPage()
    {
       this.Titolo = \"example\";
    }

    public string Titolo
    {
        get         


        
相关标签:
3条回答
  • 2020-12-18 00:59

    You have a self-referential setter. You probably meant to use auto-properties:

    public string Titolo
    {
        get;
        set;
    }
    
    0 讨论(0)
  • 2020-12-18 01:13

    Change to

    public class KPage
    {
        public KPage()
        {
           this.Titolo = "example";
        }
    
        public string Titolo
        {
            get;
            set;
        }
    }
    
    0 讨论(0)
  • 2020-12-18 01:23

    You have an infinite loop here:

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

    The moment you refer to Titolo in your code, the getter or setter call the getter which calls the getter which calls the getter which calls the getter which calls the getter... Bam - StackOverflowException.

    Either use a backing field or use auto implemented properties:

    public string Titolo
    {
        get;
        set;
    }
    

    Or:

    private string titolo;
    public string Titolo
    {
        get { return titolo; }
        set { titolo = value; }
    }
    
    0 讨论(0)
提交回复
热议问题