I have been having issues with default implementations in c# 9. Here is my problem. Each time I try to use the default implementation, I get this error 'ToyotaCorolla' does not contain a definition for 'canOffroad' and no accessible extension method 'canOffroad' accepting a first argument of type 'ToyotaCorolla' could be found (are you missing a using directive or an assembly reference?) [carApp]
Here is the interface:
public interface ICar
{
string Model{get;}
PaintColor Paint{get; set;}
string[] Passengers {get;}
void addPassenger(string passenger);
int MaxSpeed{get;}
void drive();
void openDoor();
bool canOffroad => false;
}
Here is part of the class(no canOffroad)
public class ToyotaCorolla : ICar
{
public string Model => "Toyota Corolla";
public PaintColor Paint{get; set;}
public string[] passengers = new string[6];
public virtual int MaxSpeed => 190;
public string[] Passengers => passengers;
public void addPassenger(string passenger)
{
bool added = false;
for(int i = 0 ; i < passengers.Length; i ++)
{
if (passengers[i] == null)
{
passengers[i] = passenger;
added = true;
break;
}
}
if (added == false)
{
WriteLine("The passenger could not be added(too many passengers in car)");
}
}
public void drive()
{
WriteLine("The car is driving");
}
public void openDoor()
{
WriteLine("The car door is opening");
}
}
And here is the part of the main method causing the issue.
static void Main(string[] args)
{
var JeepWrangler = new JeepWrangler();
var corola1 = new ToyotaCorolla();
WriteLine(corolla1.canOffroad);//causes compiler error
WriteLine(JeepWrangler.canOffroad);//works fine(overrides default interface method in class)
}
I have looked at many websites and it appears to look the exact same as their examples. Additionally I know I imported the namespace into the main method correctly as both corolla1 and JeepWrangler classes are in the same file as the ICar interface.
What is causing this issue?