Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement interface in derived class or through partial classes?

Tags:

c#

oop

interface

I have a partial class being generated by a tool.

Foo.cs

public partial class Foo {
    [SomeAttribute()]
    public string Bar {get;set;}
}

I need to implement the following interface for Foo without touching Foo.cs:

IFoo.cs

public interface IFoo {
    string Bar {get;set;}
}

Extending Foo is also an option, but re-implementing the Bar property isn't.

Can this be done?

like image 953
Ropstah Avatar asked Jan 18 '12 13:01

Ropstah


1 Answers

What prevents you from doing this again in another file?

public partial class Foo : IFoo
{
}

Since Bar property already exists, it wont be needed to reimplement it.

Or in a new class

public class FooExtended : Foo, IFoo
{
}

Again, you won't need to implement Bar since Foo implements it already.

like image 101
Tomislav Markovski Avatar answered Oct 03 '22 08:10

Tomislav Markovski