471,570 Members | 936 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,570 software developers and data experts.

Enforce that a class can only inherit one interface

Hi

Is it possible in one way or another to enforce that a class only inherit
from one of the derived interfaces of a base interface of some kind. For
example.
public class MyClass2 : IMyInterface1 => OK
public class MyClass2 : IMyInterface1, IMyInterface2 => Error

Regards
/Niklas

public interface IMyInterfacsBase
{
string MyMethod1();
}

public interface IMyInterface1 : IMyInterfacsBase
{
int MyInt1{ get; }
}

public interface IMyInterface2 : IMyInterfacsBase
{
int MyInt1{ get;}
int MyInt2{ get;}
}

public class MyClass2 : IMyInterface1, IMyInterface2
{
private int _MyInt1 = 4;
private int _MyInt2 = 5;

public int MyInt1
{
get { return _MyInt1; }
}

public int MyInt2
{
get { return _MyInt2; }
}

public string MyMethod1()
{
throw new NotImplementedException();
}
}
Feb 22 '06 #1
2 1794
Hi,

"Niklas" <Ni****@discussions.microsoft.com> wrote in message
news:5A**********************************@microsof t.com...
Hi

Is it possible in one way or another to enforce that a class only inherit
from one of the derived interfaces of a base interface of some kind. For


First of all, a class do not inherit an interface, it implement it. which is
different.

And no, there is no way I know of to especify that a class can implement at
the most one nterface.

If you substitute interface by class you can be sure of that, as C# ins a
single inheritance language.

why you want to do this anyway?

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
Feb 22 '06 #2
"Niklas" <Ni****@discussions.microsoft.com> a écrit dans le message de news:
5A**********************************@microsoft.com...

| Is it possible in one way or another to enforce that a class only inherit
| from one of the derived interfaces of a base interface of some kind. For
| example.
| public class MyClass2 : IMyInterface1 => OK
| public class MyClass2 : IMyInterface1, IMyInterface2 => Error

No, but you can delegate an interface to an aggregated object inside the
main class, so that each interface's behaviour is looked after separately,
even though the class implements more than one interface.

What are you really trying to achieve ?

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer
Feb 22 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.