Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Interface inheritance

Tags:

c#

interface

If i have an interface:

interface IFoo
{
    int Offset {get;}
}

can i have this:

interface IBar: IFoo
{   
    int Offset {set;}
}

so consumers of IBar will be able to set or get?

like image 336
leora Avatar asked Jan 17 '09 12:01

leora


1 Answers

No, you can't!

(I was about to write "Yes", but after reading Anthony's post, and trying out a few tweaks, I found the answer to be NO!)

class FooBar : IFoo, IBar
{
    public int Offset{get;set;}
}

(Will generate a warning as Anthony points out, which can be fixed by adding the "new" keyword.)

When trying out the code:

IBar a = new FooBar();
a.Offset = 2;
int b = a.Offset;

The last line will generate a compile error, since you have hidden IBar's Offset setter.

EDIT: Fixed the accesibillity modifier on the property in the class. Thx Anthony!

like image 99
Arjan Einbu Avatar answered Oct 04 '22 04:10

Arjan Einbu