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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T03:04:17+00:00 2026-06-08T03:04:17+00:00

I have an interface interface IInterface<E>{ E Foo(); } I then create a class

  • 0

I have an interface

interface IInterface<E>{
    E Foo();
}

I then create a class like this

class Bar : IInterface<String>, IInterface<Int32> {
}

This actually works out quite well, except I need to define one of the two functions with the interface explicit, like this:

class Bar : IInterface<String>, IInterface<Int32> {
    String Foo();
    Int32 IInterface<Int32>.Foo();
}

The drawback is that I have to do a cast every time I want to reach the Foo() which has the explicit interface.

What are the best practices when dealing with this?

I’m doing a very performance dependent application, so I really don’t want to do a million casts per second. Is this something the JIT will figure out, or should I store a casted version of the instance in itself?

I have not tried this specific code, but it looks awfully close to what I am doing.

  • 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-08T03:04:18+00:00Added an answer on June 8, 2026 at 3:04 am

    You cannot override by return type, but if you want to avoid casting you could turn your return type into an out parameter:

    interface IInterface<E> {
        void Foo(out E result);
    }
    
    class Bar : IInterface<string>, IInterface<int> {
        public void Foo(out string result) {
            result = "x";
        }
        public void Foo(out int result) {
            result = 0;
        }
    }
    
    static void Main(string[] args) {
        Bar b = new Bar();
        int i;
        b.Foo(out i);
        string s;
        b.Foo(out s);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Assume that I have this piece of code: @interface Foo : NSObject { Bar
I have a class like so: public abstract class ClassA<T> { protected ClassA(IInterface interface)
I have seen a lot of code like this header.h @interface Foo : NSObject
If I have: class foo implements Cloneable and then do: bar = new foo();
This is a little contrived, but say I have a class interface like this:
I have interface Foo public interface Foo { public void test(); } Class FooChild
I have next interface public interface IMyInterface { string this[string key] { get; set;
Suppose I have an interface interface Foo<T> { void foo(T x); T bar() }
Let's say I have a method public void Foo(string bar) that the caller should
So I have a class defined in Foo.h, implemented in Foo.m. I then have

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.