-1

I'm a starter in C#. Below is my code for set of simple operations using an object and list

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

    }
}

}

When running the above code i'm getting an exception cannot evaluate expression because a thread is in a stack overflow state in the following line.

return (Id_); . inside the function public int Id_

I cannot find any infinite loop that is causing the stack overflow in this case. What am i doing wrong here?

1
  • All your properties are returning/setting themselves, there's your loop. And what's with the underscores? Commented Jul 13, 2014 at 13:34

2 Answers 2

3

That's because your Id_ getter/setter are calling themselves.

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

What you should be doing is having a private backing field, and having the property get/set the field.

private int _id;

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

Or, since you don't seem to have any custom logic, you could use an auto-implemented property, which will automatically generate the private backing field for you.

public int Id {get;set;}

As a side note (but an important one), the naming convention in C# is to name properties using PascalCase (i.e., SomeProperty) and private fields with camelCase preceded by an underscore (_someField)

Sign up to request clarification or add additional context in comments.

1 Comment

Or even simpler to read: public int Id { get; set; }
1

It's because in your getter and setter for Id_, you're returning the property Id_ instead of the field Id.

Since inside the property you're trying to get the property again, it infinitely recurses on the get of Id_, causing the stack overflow.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.