ProfessionalCSharp/ProfessionalCSharp2021

Typo in the code sample in Event section

Closed this issue · 1 comments

zpfdev commented

On page 190, the Event Publisher section, the below code snippet was provided:

public class CarInfoEventArgs: EventArgs
{
     public CarInfoEventArgs(string car) => Car = car;
     public string Car { get; }
}

public class CarDealer
{
     public event EventHandler<CarInfoEventArgs>? NewCarInfo;
     public void CreateANewCar(string car)
     {
     Console.WriteLine($"CarDealer, new car {car}");
     RaiseNewCarCreated(car);
     }
     private void RaiseNewCarCreated(string car) =>
     NewCarCreated?.Invoke(this, new CarInfoEventArgs(car));
}

As you can see in the CarDealer's class, there isn't such EventHandler called NewCarCreated, instead, it is called NewCarInfo.

In the Event Listener section on page 191, a code snippet was provided to demonstrate the usage of publisher/consumer relationship

    CarDealer dealer = new();
    Consumer sebastian = new("Sebastian");
    dealer.NewCarInfo += sebastian.NewCarIsHere;
    dealer.NewCar("Williams");
    Consumer max = new("Max");
    dealer.NewCarInfo += max.NewCarIsHere;
    dealer.NewCar("Aston Martin");
    dealer.NewCarInfo -= sebastian.NewCarIsHere;
    dealer.NewCar("Ferrari");

However, NewCar is not a valid method in CarDealer class, it should be CreateANewCar() instead.

The file updates contains updates on issues in the book.

Page 190 with the NewCareCreated event name was already listed.

I've added the issue you mentioned with page 191. Thanks for reporting.