由于get函数中的Stack溢出,无法计算表达式

时间:2014-07-13 13:32:21

标签: c# exception stack-overflow

我是C#的首发。下面是我使用对象和列表

的简单操作集的代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ProductDetails
{


class Product
{
    int Id;
    string Name;
    int Units;
    double Price;
    public Product()
    { }
    public Product(int Id, string Name, int Units, double Price)
    {
        this.Id = Id;
        this.Name = Name;
        this.Units = Units;
        this.Price = Price;
    }
    public int Id_
    {
        get
        {
            return (Id_);
        }
        set
        {
            Id_ = value;
        }
    }
    public string Name_
    {
        get
        {
            return (Name_);
        }
        set
        {
            Name_ = value;
        }
    }
    public int Units_
    {
        get
        {
            return (Units_);
        }
        set
        {
            Units_ = value;
        }
    }
    public double Price_
    {
        get
        {
            return (Price_);
        }
        set
        {
            Price_ = value;
        }
    }


}


class Program
{
    static void Main(string[] args)
    {
    List<Product> list = new List<Product>();
    int choice;
    int ID,Units;
    string Name;
    double Price;
    do
    {
        Console.WriteLine("enter the choice from the menu");
        Console.WriteLine("------------MENU-----------");
        Console.WriteLine("1.ADD PRODUCT");
        Console.WriteLine("2.CHANGE UNITS IN STOCK AND PRICE");
        Console.WriteLine("3.DELETE PRODUCT");
        Console.WriteLine("4.VIEW PRODUCTS");
        Console.WriteLine("5.SEARCH PRODUCTS BASED ON PRICE");
        Console.WriteLine("6.EXIT");
        choice = Convert.ToInt32(Console.ReadLine());
        switch (choice)
        {
            case 1: Console.WriteLine("Enter the product details ID,Name,Units and Price");
                ID = Convert.ToInt32(Console.ReadLine());
                Name = Console.ReadLine();
                Units = Convert.ToInt32(Console.ReadLine());
                Price = Convert.ToDouble(Console.ReadLine());
                Product obj1 = new Product(ID, Name, Units, Price);
                list.Add(obj1);
                break;
            case 2:
                Console.WriteLine("enter the product ID");
                ID = Convert.ToInt32(Console.ReadLine());

                Console.WriteLine("enter the new number of stocks and price");
                Units = Convert.ToInt32(Console.ReadLine());
                Price = Convert.ToDouble(Console.ReadLine());
                foreach (Product item in list)
                {
                    if (item.Id_ == ID)
                    {
                        item.Units_ = Units;
                        item.Price_ = Price;
                    }
                }
                break;
            case 3:
                Console.WriteLine("enter the ID");
                ID = Convert.ToInt32(Console.ReadLine());
                foreach (Product item in list)
                {
                    if (item.Id_ == ID)
                    {
                        list.Remove(item);
                    }
                }
                break;
            case 4:
                foreach (Product item in list)
                {
                    Console.WriteLine("ID:" + item.Id_);
                    Console.WriteLine("Name:" + item.Name_);
                    Console.WriteLine("Units:" + item.Units_);
                    Console.WriteLine("Price:" + item.Price_);
                }
                break;
            case 5:
                Console.WriteLine("enter the search price");
                Price = Convert.ToDouble(Console.ReadLine());
                foreach (Product item in list)
                {
                    if (item.Price_ > Price)
                    {
                        Console.WriteLine("Name:" + item.Name_);
                        Console.WriteLine("Price:" + item.Price_);
                    }
                }
                break;
        }
    } while (choice != 6);

    }
}

}

运行上面的代码时,我在下一行中收到异常cannot evaluate expression because a thread is in a stack overflow state

return (Id_);。在函数public int Id_

在这种情况下,我找不到任何导致堆栈溢出的无限循环。我在这做错了什么?

2 个答案:

答案 0 :(得分:3)

那是因为你的Id_ getter / setter正在调用自己。

public int Id_
{
    get
    {
        return (Id_);
    }
    //...
}

应该做的是拥有私人支持字段,并让属性获取/设置字段

private int _id;

public int Id
{
    get { return _id; }
    set { _id = value; }
}

或者,由于您似乎没有任何自定义逻辑,您可以使用自动实现的属性,该属性将自动为您生成私有支持字段。

public int Id {get;set;}

作为旁注(但重要的一点),C#中的naming convention是使用 PascalCase (即SomeProperty)命名属性,使用 camelCase 前面有下划线(_someField

答案 1 :(得分:1)

这是因为在Id_的getter和setter中,您将返回属性Id_而不是字段Id

由于你在属性中尝试再次获取属性,它会在get的{​​{1}}上无限地递归,导致堆栈溢出。