Hello, I’m working on this task:
Create a class Car. Inside the class, create a public static int field CarsCount and increment it in the constructor.
In the Main method, create 5 objects of the type Car. Then, output a string containing the number of cars created to the screen, “Number of created cars is {CarsCount}”, using the value of the static field for it.
Here is my code:
namespace Static
{
class Car
{
public static int CarsCount;
public Car()
{
CarsCount++;
}
}
class HowManyCars
{
public static void Main()
{
Car car1 = new Car();
Car car2 = new Car();
Car car3 = new Car();
Car car4 = new Car();
Car car5 = new Car();
Console.WriteLine($"Number of created cars is {0}", Car.CarsCount);
}
}
}
Can anyone please help me to solve it?