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?