static void Main(string[] args) { Car c = new Car("Frank", 50, "red"); c.DisplayStats(); Garage g = new Garage(); Console.WriteLine("=> auto properies are initialized to defaults"); Console.WriteLine("int: {0}", g.NumberOfCars); Console.WriteLine("object is null {0}", g.MyAuto == null); Console.ReadLine(); }
static void Main(string[] args) { Car c = new Car(); c.PetName = "Frank"; c.Speed = 55; c.Color = "red"; Console.WriteLine("your car is named:{0}",c.PetName); c.DisplayStats(); Garage g = new Garage(); g.myAuto = c; Console.WriteLine("Number of cars:{0}",g.NumberOfCars); Console.WriteLine(g.myAuto.PetName); Console.ReadLine(); }
static void Main(string[] args) { Console.WriteLine("**** Fun with Automatic Properites ****\n"); Car c = new Car(); c.PetName = "Frank"; c.Speed = 55; c.Color = "Red"; c.DisplayStats(); Garage g = new Garage(); g.MyAuto = c; Console.WriteLine("Number of Cars in garage: {0}", g.NumberOfCars); Console.WriteLine("Your car is named: {0}", g.MyAuto.PetName); Console.ReadLine(); }
static void Main(string[] args) { Console.WriteLine("***** Fun with auto props *****\n"); Car c = new Car(); c.PetName = "Frank"; c.Speed = 55; c.Color = "Red"; //Console.WriteLine("Your car is named {0}? That's odd...",c.PetName); c.DisplayStats(); //put car in the garage. Garage g = new Garage(); g.MyAuto = c; //0OK, prints default value of 0. Console.WriteLine("Number of cars in garage: {0}", g.NumberOfCars); Console.WriteLine("Your car is named: {0}", g.MyAuto.PetName); //runtime error! backing field is currently null! //Console.WriteLine(g.MyAuto.PetName); Console.ReadLine(); }
public Garage( Car car, int number ) { MyAuto = car; NumberOfCars = number; }
// Must use constructors to override default // values assigned to hidden backing fields. public Garage() { MyAuto = new Car(); NumberOfCars = 1; }
public Garage(Car car, int number) { MyAuto = car; NumberOfCars = number; }