Interface, classes and inheritance

D

Dansk

Hi all,

I've just found a strange (for me) family relationship with classes and
interface.
Here is a simple code sample :

namespace Test
{
public interface IFoo
{
void Foo();
};

public class A
{
public void Foo()
{
}
};

public class B : A, IFoo
{
};
}

The C# compiler seems happy and after some tests and debugging, all
"seems" ok.

What do you think about that ?
Elegant pattern or coding horror ?

Thanks for your advices.
 
J

Jon Skeet [C# MVP]

What do you think about that ?
Elegant pattern or coding horror ?

I guess the main question is: "Does the meaning of IFoo.Foo() match
(exactly) the meaning of A.Foo()?"

If it does, and it always will, then it's okay. A little odd, but
okay.

However, if A doesn't know anything about IFoo, then quite often I'd
expect the meanings to be subtly different.

Jon
 
M

Marc Gravell

Seems OK to me... granted it would be more natural to have the ":
IFoo" and the implementing "Foo()" member in the same Type... but if
the author of B wasn't happy with this, they can always use explicit
implementation:

void IFoo.Foo() {
// do something else...
}

Marc
 
J

Jon Skeet [C# MVP]

Seems OK to me... granted it would be more natural to have the ":
IFoo" and the implementing "Foo()" member in the same Type...

I'm *guessing* the situation is that A is a 3rd party class (e.g.
something from the framework) and both B and IFoo are custom types the
OP owns. If it so happens that the natural interface for IFoo contains
an appropriate member which is defined in A, there's no reason for B
to go to the trouble of reimplementing it.

That's my guess, anyway :)

Jon
 
A

Arne Vajhøj

Dansk said:
I've just found a strange (for me) family relationship with classes and
interface.
Here is a simple code sample :

namespace Test
{
public interface IFoo
{
void Foo();
};

public class A
{
public void Foo()
{
}
};

public class B : A, IFoo
{
};
}

The C# compiler seems happy and after some tests and debugging, all
"seems" ok.

What do you think about that ?
Elegant pattern or coding horror ?

public class B : A
{
};

should have the same functionality and look more natural, but small
detail.

Why do you consider it special ?

Arne
 
D

Dansk

Hi Jon, thanks for your answer, I had the same thoughts.
Unfortunatly, I can't ask the people that wrote Ifoo.Foo() nor A.Foo()
if the meaning is the same.
And even if it is, I'm still not very confident with that.

To be able to trust the code, I changed it in order to make A implement
IFoo, as shown below :

namespace Test
{
public interface IFoo
{
void Foo();
};

public class A : IFoo
{
public void Foo()
{
}
};

public class B : A, IFoo
{
};
}


This looks more confortable for my mind.
Unfortunatly, we run FxCop on our assemblies, and it refuses to analyse
that one with that modification, complaining about "Analysis error,
Assembly references error"


:(


Jon Skeet [C# MVP] a écrit :
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top