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

本文关键字:计算 表达式 栈溢出 堆栈 get 函数 由于 | 更新日期: 2023-09-27 18:12:46

我是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_

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

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

这是因为您的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#中的命名约定是使用PascalCase(即SomeProperty)和私有字段camelCase前面有下划线(_someField)
来命名属性。

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

由于在属性中您试图再次获得该属性,它在Id_get上无限递归,导致堆栈溢出