Jump to content
  • Advertisement
Sign in to follow this  
CyberSlag5k

C# - Multi-interface field

This topic is 3499 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Let's say I have two interfaces, IOne and ITwo, and I want some other class to have a class property that subscribes to both. Is the best approach: 1.
public class MyClass<T> where T : IOne, ITwo
{
   public T MyProperty { get; set; }
}

2.
interface IOneAndTwo : IOne, ITwo
{
}

public class MyClass
{
   public IOneAndTwo MyProperty  { get; set; }
}

3. Something else. I'm not positive that #2 will work, but I think so. The idea is to accomplish something like the following:
public class MyOtherClass : IOne, ITwo
{
// stuff
}

MyClass myClass = new MyClass();
MyOtherClass myOtherClass = new MyOtherClass();
myClass.MyProperty = myOtherClass

Thoughts?

Share this post


Link to post
Share on other sites
Advertisement
In plain C#, a class must inherit from both interfaces to be considered an instance that satisfies them. If you want to inherit directly from the interfaces or create a third interface that inherits from the two (and is then used/inherited by others), that's a design choice that depends on the usage.

The generic should work like that too, but again depends on the usage.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!