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 6833085
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T22:56:12+00:00 2026-05-26T22:56:12+00:00

How can I have a type reference that refers to any object that implements

  • 0

How can I have a type reference that refers to any object that implements a set of interfaces?

For example, I can have a generic type like this:

Java:

public class Foo<T extends A & B> { }

C#

public class Foo<T> where T : A, B { }

That’s how to have a class-wide generic type. However, I’d like to simply have a data member which references any object that extends a given set of interfaces.

Example:

public class Foo
{
    protected <? extends A, B> object;

    public void setObject(<? extends A, B> object)
    {
        this.object = object;
    }
}

If it’s possible to have this sort of type syntax, how could I do it in both Java and C#?


I realize I can just create another interface that extends all desired interfaces. However, I don’t see this as optimal, as it needlessly adds another type whose sole purpose is to get around syntax. Granted this is a very minor issue, but in terms of elegance it’s a bit galling.

  • 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-26T22:56:12+00:00Added an answer on May 26, 2026 at 10:56 pm

    In C#, you can use a tuple to store the value in a kind of superposition:

    public class Foo {
      private Tuple<IA, IB> junction;
      public void SetValue<T>(T value) where T : IA, IB {
        junction = Tuple.Create<IA, IB>(value, value);
      }
    }
    

    You can also have a specialized class to enforce the constraint that both values reference the same object:

    public class Junction {
      public IA A { get; private set; }
      public IB B { get; private set; }
      private Junction() { }
      public static Junction Create<T>(T value) where T: IA, IB {
        return new Junction {
          A = value,
          B = value
        };
      }
    }
    
    public class Foo {
      private Junction junction;
      public void SetValue<T>(T value) where T : IA, IB {
        junction = Junction.Create(value);
      }
    }
    

    In Java, a wildcard would simplify things a little:

    class Junction<E extends A & B> {
      private final E value;
      public Junction(E value) {
        this.value = value;
      }
      public E getValue() {
        return value;
      }
    }
    
    class Foo {
      private Junction<?> junction;
      public <E extends A & B> void setValue(E value) {
        junction = new Junction<E>(value);
      }
    }
    

    Or you can have aliases to the same value (C#, but also applicable to Java):

    public class Foo {
      private IA a;
      private IB b;
      public void SetValue<T>(T value) where T : IA, IB {
        a = value;
        b = value;
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If I have a multi-threaded program that reads a cache-type memory by reference. Can
I'm looking for a kind of array data-type that can easily have items added,
I have an HTML form where the user can type in any number:5, 8,
I have a menu with submenus that can be toggled (hide/show type deal). Is
I have two collections of my own reference-type objects that I wrote my own
I have my type x of type varchar(50). How can I alter it to
In .NET, a value type (C# struct ) can't have a constructor with no
I have a textfield where the user can type what ever he want into
I have a data in NSMutableData type. Can I convert the data to a
I have a simple extension method on the int type so I can do

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.