So i am currently learning about events and delegates and wanted to make some testing. I created a class called car. My idea was that everytime i change the value of its tires i want to invoke the event. This is all only for learning purposes. I get an StackoverflowException and i can not figure out why.
private static void Main(string[] args)
{
var Car = new Car { Tire_Value = 100 };
Car.OnTireChanged += Tirestatus;
Car.Tire_Value = 10;
}
public class Car
{
public event EventHandler OnTireChanged;
public int Tire_Value
{
get
{
return this.Tire_Value;
}
set
{
this.Tire_Value = value;
this.OnTireChanged?.Invoke(this, EventArgs.Empty);
}
}
}
private static void Tirestatus(object sender, EventArgs args)
{
var car = (Car)sender;
if (car.Tire_Value < 40) Console.WriteLine("Tire is damaged");
}