Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 770547
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T18:31:01+00:00 2026-05-14T18:31:01+00:00

Similar to this Java question . I would like to specify that a variable

  • 0

Similar to this Java question. I would like to specify that a variable implements multiple interfaces. For instance

private {IFirstInterface, ISecondInterface} _foo;

public void SetFoo({IFirstInterface, ISecondInterface} value)
{
    _foo = value;
}

Requirements:

  • I don’t have the ability to add an interface to most type that would be passed in to Foo. So I can’t create a third interface that inherits from IFirstInterface and ISecondInterface.
  • I would like to avoid making the containing class generic if possible because the type of Foo doesn’t have much to do with the class and the user isn’t likely to know it at compile time.
  • I need to use foo to access methods in both interfaces at a later time.
  • I would like to do this in a compiler safe way, i.e. no trying to cast to the interface just before trying to use it. If foo does not implement both interfaces quite a bit of functionality won’t work properly.

Is this possible?

Edit: I’ve wanted this a few times for various reasons. In this instance it’s because I am changing a set of properties from ObservableCollections to ReadOnlyObservableCollections. I have a helper class that creates a projection from a source ObservableCollection to another Collection. Since ReadOnlyObservableCollection does not inherit from ObservableCollection and I only need operations in IList and INotifyCollectionChanged I was hoping to store my source collection as a combination of these two interfaces rather than requiring an ObservableCollection.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T18:31:02+00:00Added an answer on May 14, 2026 at 6:31 pm

    If you want to constrain your method SetFoo to take only parameters that implement these two interfaces, then you are in luck:

    public void SetFoo<T>(T value) where T : IFirstInterface, ISecondInterface
    {
        _foo = value;
    }
    

    From this point on, any time you want to access your _foo member as one of either of these two interfaces, you’ll just have to access it via casts: (IFirstInterface)_foo or (ISecondInterface)_foo, respectively.

    You did say you’d like to avoid resorting to casts for compile-time safety; but I think, if you figure out a way to ensure that _foo gets initialized using SetFoo above, you can cast all you want with peace of mind.


    I just realized the below describes something you specifically stated in the question you didn’t want to do. So, I say, go with the above.

    Another idea would be, since it looks like this _foo object is a class member (I’m basing this assumption on the _ in the variable name), you could define your class in this way:

    class YourClassThatHasAFoo<T> where T : IFirstInterface, ISecondInterface {
        private T _foo;
    
        public void SetFoo(T value) {
            _foo = value;
        }
    }
    

    Whether this actually makes sense depends, obviously, on your specific scenario. You’ll definitely have to think it through, as classes with constraints like this sometimes lead to issues you might not have expected down the road (such as that SetFoo now must take a parameter of type T, not just any class that implements your two interfaces).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 406k
  • Answers 406k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer lists:keyfind/3 was introduced in OTP/R13A. I suspect you are using… May 15, 2026 at 6:18 am
  • Editorial Team
    Editorial Team added an answer Use the #error directive: #if SHOULD_FAIL #error "bad compiler!" #endif… May 15, 2026 at 6:18 am
  • Editorial Team
    Editorial Team added an answer You generally want to express something that is a state… May 15, 2026 at 6:18 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.