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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T05:53:30+00:00 2026-05-28T05:53:30+00:00

I have following code: public abstract class Operand<T> { public T Value { get;

  • 0

I have following code:

public abstract class Operand<T>
{
    public T Value { get; protected set; }

    public bool IsEmpty { get; protected set; }

    public override string ToString()
    {
        return IsEmpty ? Value.ToString() : string.Empty;
    }
}
public class DoubleOperand : Operand<Double> {}

public interface IOperandFactory<T>
    {
        Operand<T> CreateEmptyOperand();
        Operand<T> CreateOperand(T value);
    }

public class DoubleFactory: IOperandFactory<double>
{
    public Operand<Double> CreateEmptyOperand()
    {
        //implementation
    }

    public Operand<Double> CreateOperand(double value)
    {
        //implementation
    }
}

I simlified code to just show the structure.
Now I need associationDictionary that will return IOperandFactory for required Type:
Something like this:

var factoryDict = 
new Dictionary<Type, IOperandFactory<>>() { { typeof(double), new DoubleFactory() } };

Could you help me to achieve it if it is possible?

  • 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-28T05:53:30+00:00Added an answer on May 28, 2026 at 5:53 am

    To do that, you would need to have a non-generic interface (typically in addition to the generic interface), i.e. a non-generic Operand, with Operand<T> : Operand (could also be an interface), and a non-generic IOperandFactory with IOperandFactory<T> : IOperandFactory. The only other option is to store a Dictionary<Type, object>, and have the caller cast as necessary.

    Here’s the non-generic approach:

    using System.Collections.Generic;
    using System;
    public interface IOperand
    {
        object Value { get; }
        bool IsEmpty { get; }
    }
    public abstract class Operand<T> : IOperand
    {
        public T Value { get; protected set; }
        object IOperand.Value { get { return Value; } }
        public bool IsEmpty { get; protected set; }
        public override string ToString()
        {
            return IsEmpty ? Value.ToString() : string.Empty;
        }
    }
    public class DoubleOperand : Operand<double> { }
    
    public interface IOperandFactory
    {
        IOperand CreateEmptyOperand();
        IOperand CreateOperand(object value);
    }
    public interface IOperandFactory<T> : IOperandFactory
    {
        new Operand<T> CreateEmptyOperand();
        Operand<T> CreateOperand(T value);
    }
    
    public class DoubleFactory : IOperandFactory<double>
    {
        public Operand<double> CreateEmptyOperand()
        {
            throw new NotImplementedException();
        }
        IOperand IOperandFactory.CreateEmptyOperand() {
            return CreateEmptyOperand();
        }
    
        IOperand IOperandFactory.CreateOperand(object value) {
            return CreateOperand((double)value);
        }
        public Operand<double> CreateOperand(double value)
        {
            throw new NotImplementedException();
        }
    }
    
    static class Program
    {
        static void Main()
        {
            var factoryDict = new Dictionary<Type, IOperandFactory> {
              {typeof (double), new DoubleFactory()}
            };
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If have following class public abstract class MyBaseClass : BaseClass { public override string
I have the following code: public abstract class A<T extends B<? extends A<T>>>{ @Override
I have following code public class TEST { public static void main(String arg[]){ try
I have the following code: public class AppDomainArgs : MarshalByRefObject { public string myString;
I have a question about .net generics. Consider the following code: public abstract class
Within my code a have the following abstract superclass public abstract class AbstractClass<Type extends
I have following code: public abstract class TestProperty { public abstract Object PropertyValue {
I have the following code: public abstract class BaseController : Controller { public IUserService
I have the following code public abstract class Event { public void fire(Object... args)
I have the following class: public abstract class AbstractParent { static String method() {

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.