Additional C # arguments in an abstract method

Given:

interface IFoo
{
    void Print(string text = "abc");
}

class Bar : IFoo
{
    public void Print(string text = "def")
    {
        Console.WriteLine(text);
    }
}

class Program
{
    static void Main(string[] args)
    {
        Bar b = new Bar();
        b.Print();

        IFoo f = b as IFoo;
        f.Print();
    }
}

Conclusion:

def
abc

Just me or is it a little weird? Initially, I expected "def" in both cases. However, if that were the case, then the optional arguments of abstract methods would be useless. But still it seems like a good starting point for unpleasant mistakes.

+3
source share
1 answer

- , , . , , . f , , , .

+6

Source: https://habr.com/ru/post/1765449/


All Articles