1

There are 2 interfaces defined as:

interface calc1
{
    int add(int a, int b);
}
interface calc2
{
    int add(int x, int y);
}

A class implements both these interfaces as:

class Calculation : calc1, calc2
{
    public int result1;
    public virtual int add(int a, int b)
    {
        return result1 = a + b;
    }        
}

Everything works fine. But when I change class definition as:

class Calculation : calc1, calc2
{
    public int result1;
    public int result2;
    public virtual int calc1.add(int a, int b)
    {
        return result1 = a + b;
    }

    public int calc2.add(int x, int y)
    {
        return result2 = x - y;
    }
}

I see errors:

The modifier 'virtual' is not valid for this item

and

The modifier 'public' is not valid for this item

What is wrong in the latter class code?

gliese 581 g
  • 349
  • 1
  • 4
  • 17
  • Why do you need the method to be `virtual`? are you planning on inheriting from `Calculation`? Do you only need one of the two methods to be virtual? – Yacoub Massad Jul 06 '16 at 10:07
  • Yes. As I cannot post the original code I have altered it before posting over here. I only want one of the 2 methods to be virtual as I am going to override it in derived class. – gliese 581 g Jul 06 '16 at 10:10
  • You can make the first one an implicit implementation (`public virtual int add(int a, int b)`) and the second one an explicit implementation (`int calc2.add(int x, int y)`) – Yacoub Massad Jul 06 '16 at 10:13
  • Thanks! It worked. – gliese 581 g Jul 06 '16 at 10:15

1 Answers1

2

Explicit interface implementations can't have an access modifier, since they are only visible when using the interface as a type. There is no point in making it public.

Also, you can't derived from an explicit interface implementation, so virtual is useless too. The compiler knows that and breaks on it.

Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325