任何人都可以找出这个StackOverflowException的原因吗?

时间:2016-01-18 15:02:36

标签: c# stack-overflow

试图学习编程并再次失去信心,因为我认为我已经内化了一个简单的概念,但似乎发生了一些看似非同寻常的事情,或者它只是飞过我的脑袋。

当我运行程序时,如果通过为FirstName或SecondName指定字符串来访问该属性,则会出现StackOverFlowException

我的客户类别:

class Customer : ICustomer
{
    public string FirstName
    {
        get
        {
            return FirstName;
        }

        set
        {
            FirstName = value;
        }
    }

    public string fName
    {
        get
        {
            return fName;
        }

        set
        {
            fName = value;
        }
    }

    public string SecondName
    {
        get
        {
            return SecondName;
        }

        set
        {
            SecondName = value;
        }
    }

    public string sName
    {
        get
        {
            return sName;
        }

        set
        {
            sName = value;
        }
    }

    public int ID
    {
        get
        {
            return ID;
        }

        set
        {
            ID = value;
        }
    }

    public int mId
    {
        get
        {
            return mId;
        }

        set
        {
            mId = value;
        }
    }

    public int GetID()
    {
        return mId;
    }

    public void SetID(int id)
    {
        mId = ID;
    }

    public void SetName(string fName, string sName)
    {
        fName = FirstName;
        sName = SecondName;
    }
}

和主程序

class Program
{
    /// <summary>
    /// Create unique string code based off current date and time.
    /// </summary>
    /// <returns>code string</returns>
    static string generateUniqueCode()
    {
        string characters = "abcdefghijklmnopqrstuvqxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        string ticks = DateTime.UtcNow.Ticks.ToString();
        var code = "";
        for (var i = 0; i < characters.Length; i += 2)
        {
            if ((i + 2) <= ticks.Length)
            {
                var number = int.Parse(ticks.Substring(i, 2));
                if (number > characters.Length - 1)
                {
                    var one = double.Parse(number.ToString().Substring(0, 1));
                    var two = double.Parse(number.ToString().Substring(1, 1));
                    code += characters[Convert.ToInt32(one)];
                    code += characters[Convert.ToInt32(two)];
                }
                else
                    code += characters[number];
            }
        }
        return code;
    }

    /// <summary>
    /// Creates unique integer code based off current date and time.
    /// </summary>
    /// <returns>integer code</returns>
    static int generateUniqueCode(int seed)
    {
        string characters = "0123456789";

        Random randInt = new Random(seed);
        var ticks = randInt.Next();

        int code = 0;
        for (var i = 0; i < characters.Length; i += 2)
        {
            if ((i + 2) <= ticks)
            {
                var number = ticks;
                if (number > characters.Length - 1)
                {
                    var one = double.Parse(number.ToString().Substring(0, 1));
                    var two = double.Parse(number.ToString().Substring(1, 1));
                    code += characters[Convert.ToInt32(one)];
                    code += characters[Convert.ToInt32(two)];
                }
                else
                    code += characters[number];
            }
        }
        return code;
    }

    static void Main(string[] args)
    {
        Customer customer = new Customer();

        int generatedIntCode = generateUniqueCode(1);

        customer.FirstName = "Conor";
        customer.SecondName = "MacFirbhisigh";
        customer.SetID(generatedIntCode);
        Console.WriteLine("{0}, {1} {2}", customer.ID, customer.FirstName, customer.SecondName);

        //Console.ReadKey();
    }
}

2 个答案:

答案 0 :(得分:10)

FirstName(和所有其他人)的getter和setter中,你一遍又一遍地调用同一个属性。您创建的无限循环将产生StackOverflowException

如果您不想为自己的属性添加自定义逻辑,请使用自动实现的属性:

public string FirstName
{
    get;
    set;
}

如果您确实希望自己实现该属性,创建自己的支持字段,它应该是这样的(这实际上与上面代码生成的内容相同):

private string firstName; // backing field

public string FirstName
{
    get
    {
        return this.firstName; // return the backing field
    }
    set
    {
        this.firstName = value; // set the backing field
    }
}

答案 1 :(得分:0)

你遗漏了一些重要的部分

首先,您总是需要声明变量,

public - 用于外部get和set,因此不需要get-set方法。 private - get / set方法是修改数据的必要条件。

另请参阅SetName方法。

希望有所帮助:p

这应该可以解决问题:

class Customer : ICustomer
{
    private string firstName;
    private string name;
    private string secondName;
    private string sName;
    private int iD;
    private int mId;

    public string FirstName
    {
        get
        {
            return firstName;
        }

        set
        {
            firstName = value;
        }
    }

    public string Name
    {
        get
        {
            return name;
        }

        set
        {
            name = value;
        }
    }

    public string SecondName
    {
        get
        {
            return secondName;
        }

        set
        {
            secondName = value;
        }
    }

    public string SName
    {
        get
        {
            return sName;
        }

        set
        {
            sName = value;
        }
    }

    public int ID
    {
        get
        {
            return iD;
        }

        set
        {
            iD = value;
        }
    }

    public int MId
    {
        get
        {
            return mId;
        }

        set
        {
            mId = value;
        }
    }

    public void SetName(string fName, string sName)
    {
        FirstName = fName;
        SecondName = sName ;
    }
}