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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T06:56:17+00:00 2026-05-16T06:56:17+00:00

Consider the following code sample: interface IData { int Count(); } interface IOperations {

  • 0

Consider the following code sample:

interface IData {
  int Count();
}

interface IOperations {
  IData Foo();
  double Bar(IData a);
}

class Data1 : IData {
  public int Count() { return 37; }
  public double SomethingElse { get; set; }
}

class Ops1 : IOperations 
{
  public Data1 Foo() { return new Data1(); } // want to return specific type here
  public double Bar(Data1 x) { ... } // want to get specific type here
                                     // and not use operator as everywhere
}

// more definitions of classes Data2, Ops2, Data3, Ops3, ...

// some code:
Ops1 a = new Ops1();
Data1 data = a.Foo(); // want Data1 here not IData!
double x = a.Bar(data);

I could of course just use public IData Foo() { return new Data1(); }:

// some code
Ops1 a = new Ops1();
Data1 data = a.Foo() as Data1;

but with as everywhere, the code is quickly becoming confusing.

I wonder if there is a good design pattern to achieve this in a clear and strong way?

Edit: Is is important, that Ops and Data share a common base class:

List<IOperations> ops = ...;
List<IData> data = ...;
List<double> result = ...;
for(int i=0; i<ops.Count; i++) 
  result[i] = ops[i].Bar(data[i]);

So for the case with the return type, I wonder that this is forbidden, because I satisfy the requirement of the interface. In the case with parameters probably there is some additional (template) layer required.

  • 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-16T06:56:17+00:00Added an answer on May 16, 2026 at 6:56 am

    You could use generics:

    interface IOperations<T> where T: IData
    { 
      T Foo();
      double Bar(T a);
    }
    
    class Ops1 : IOperations<Data1>
    {
        public Data1 Foo() { return new Data1(); }
        public double Bar(Data1 x) { /* ... */  } 
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider the following sample code: #include <iostream> using namespace std; class base { public:
Consider the following sample code: class SampleClass { public long SomeProperty { get; set;
Consider the following sample code: class MyClass { public long x; public void DoWork()
Consider the following sample code: class Base { public: void f(); virtual void vf();
Consider the following code: #include<iostream> using namespace std; class sample { int a; int
Consider the following sample code. #include <iostream> using namespace std; class base { public:
Consider the following code: template <class x1, class x2 = int*> struct CoreTemplate {
Consider the following code: public interface A { public A another(); } public interface
Consider the following sample code below: #include <iostream> using namespace std; class base {
Considering the following sample code: // delivery strategies public abstract class DeliveryStrategy { ...

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.