Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- CarConfiguration carConfiguration = new CarConfiguration()
- {
- CarName = "Ferrari",
- CarModel = "LaFerrari",
- Spolier = true,
- BuildDate = new DateTime(2018, 01, 01)
- };
- //Thats not work because the "conf" parameter is never assign in the Car constructor
- Car myOwnCar = new Car(conf =>
- {
- conf = carConfiguration;
- });
- Console.WriteLine(myOwnCar.CarConfigurationText());
- //That works, but is not my purpose do it like this !
- Car myOtherCar = new Car(carConfiguration);
- Console.WriteLine(myOtherCar.CarConfigurationText());
- public class CarConfiguration
- {
- public bool Spolier { get; set; } = false;
- public string CarName { get; set; } = String.Empty;
- public string CarModel { get; set; } = String.Empty;
- public DateTime BuildDate { get; set; } = default(DateTime);
- }
- public class Car
- {
- private CarConfiguration carConfiguration = null;
- //Thats not work because carConfiguration is not assigned in the Action as a reference
- public Car(Action<CarConfiguration> configureCar)
- {
- configureCar(carConfiguration);
- }
- //Thats works!
- public Car(CarConfiguration configureCar)
- {
- carConfiguration = configureCar;
- }
- public string CarConfigurationText()
- {
- StringBuilder strBuilder = new StringBuilder();
- if (carConfiguration != null)
- {
- strBuilder.AppendLine(carConfiguration.CarModel);
- strBuilder.AppendLine(carConfiguration.CarName);
- strBuilder.AppendLine(carConfiguration.Spolier.ToString());
- strBuilder.AppendLine(carConfiguration.BuildDate.ToString("mm-DD-yyyy"));
- }
- else
- {
- strBuilder.AppendLine("Car is not configure!");
- }
- return strBuilder.ToString();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement