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

  • Home
  • SEARCH
  • 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 7911595
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T13:18:26+00:00 2026-06-03T13:18:26+00:00

Let’s say I want to express a method that expect an object which implements

  • 0

Let’s say I want to express “a method that expect an object which implements interfaces IFoo and IBar“. Something like:

void Method(IFoo+IBar param);

How can I say this using C# ?

(Is there a syntaxical construct? Or a good idiom perhaps?)


An attempt

Introduce an interface:

interface IFooBar : IFoo, IBar {}

void Method(IFooBar param);

This is bad and I regret even thinking of it 🙂 Looks OK at the first glance, but the sad part is that silently introduces a dependency which shouldn’t be here.

The sole reason for IFooBar to exist is to be a part of Method‘s interface. Hence, any class of objects to be used as Method‘s parameter must know that this method (and that interface) exist – which wouldn’t be the case otherwise. All classes implementing IFoo and IBar would need to be modified to also implement IFooBar (possibly getting to know a new assembly) – which is heavily impractical, or even impossible if we can’t modify them.

Unnecessary dependency = a no-go.

A workaround

Give up static typing:

void Method(object param)
{
    if (!param is IFoo)
        throw new ArgumentException("IFoo not supported", "param");
    if (!param is IBar)
        throw new ArgumentException("IBar not supported", "param");

    // ...
}

(or: define one type in signature and dynamically check the others – preferable if one is the most important, but even more confusing)

Works correcty, but in run-time (no compile-time check). Desperately requires a doc (and everyone to read it).

Also only practical in function parameters. The code’d get massively bloated with casts if I tried to use that on a field.


A real case for this situation? For instance IList<Foo> + INotifyCollectionChanged.

  • 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-06-03T13:18:28+00:00Added an answer on June 3, 2026 at 1:18 pm
    public void someMethod<T>(T param) where T : IFoo, IBar
    {...}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say that I have a model that handles recipes, and I want to
Let's say I have this string which I want to put in a multidimensional
Let's say that I have a method with the signature: public static bool ValidDateToSend(DateTime
Let's say the Activity I want to start is named OccupyThePieShop I was previously
Let's say I can call a method like this: core::get() . What is the
Let's say I have the following object: var VariableName = { firstProperty: 1, secondProperty:
Let's say I'm making a tool to help epicureans keep track of which delicacies
Let's say I have multiple requirements for a password. The first is that the
Let's say that I have a date in R and it's formatted as follows.
Let's say that I have a set of relations that looks like this: relations

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.