类型的未处理的异常'System.StackOverflowException“发生 [英] An unhandled exception of type 'System.StackOverflowException' occurred

查看:813
本文介绍了类型的未处理的异常'System.StackOverflowException“发生的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么呢?这是我的code:

 公共类KPage
{
    公共KPage()
    {
       this.Titolo =榜样;
    }

    公共字符串TITOLO
    {
        {返回TITOLO; }
        集合{TITOLO =价值; }
    }
}
 

我在构造函数中设置数据。所以,我想要做的有些事情就像

  KPage页=新KPage();
回复于(page.Titolo);
 

但我得到这个错误:

 集合{TITOLO =价值; }
 

解决方案

您有一个无限循环的位置:

 公共字符串TITOLO
{
    {返回TITOLO; }
    集合{TITOLO =价值; }
}
 

您参考 TITOLO 在code的时刻,getter或setter致电这就要求它调用它调用该函数调用吸气,吸气,吸气,吸气吸气......巴姆 - StackOverflowException

要么使用一个支持字段或使用自动实现的属性

 公共字符串TITOLO
{
    得到;
    组;
}
 

或者

 私人字符串TITOLO;
公共字符串TITOLO
{
    {返回TITOLO; }
    集合{TITOLO =价值; }
}
 

Why this? This is my code :

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

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

I set data by the constructor. So, I'd like to do somethings like

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

but I get that error on :

set { Titolo = value; }

解决方案

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

这篇关于类型的未处理的异常'System.StackOverflowException“发生的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆