0

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?

Panagiotis Kanavos
  • 120,703
  • 13
  • 188
  • 236
  • 1
    Side note: please review [MCVE] guidance on posting code - right now there is no way to know what `corolla1` is nor how `JeepWrangler` constant/class look like. If duplicate does not answer your question makes sure to [edit] the question to provide above details as well as explanation why duplicate was not enough. – Alexei Levenkov Jan 19 '21 at 17:33
  • DIMs work. You have to cast to the interface to use its implementation though. That's no different than explicit interface implementations. If you used `ICar corola1 = new ToyotaCorolla();` there would be no compile error – Panagiotis Kanavos Jan 19 '21 at 17:51
  • Thank you for the help. – Daniel Kohn Jan 19 '21 at 17:54
  • @PanagiotisKanavos Thank you very much. I made the change but this causes an issue in another part of my code not shown in code above `foreach(var element in corola1.passengers)`. I think this is due to the fact that the passengers field is not part of the interface and fields cannot be added to interfaces. Is their anyway to circumvent this? – Daniel Kohn Jan 19 '21 at 17:58
  • @DanielKohn why do you use a public field when you already have a public property? That's the real bug. Fields are implementation details, private or not. Having a get-only property in an interface doesn't prevent implementers from having a property with a setter – Panagiotis Kanavos Jan 19 '21 at 18:05

0 Answers0