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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T14:30:25+00:00 2026-05-14T14:30:25+00:00

The problem: class StatesChain : IState, IHasStateList { private TasksChain tasks = new TasksChain();

  • 0

The problem:

class StatesChain : IState, IHasStateList {
    private TasksChain tasks = new TasksChain();

     ...

    public IList<IState> States {
        get { return _taskChain.Tasks; }
    }

    IList<ITask> IHasTasksCollection.Tasks {
        get { return _taskChain.Tasks; } <-- ERROR! You can't do this in C#!
                                             I want to return an IList<ITask> from
                                             an IList<IStates>.
    }
}

Assuming the IList returned will be read-only, I know that what I’m trying to achieve is safe (or is it not?). Is there any way I can accomplish what I’m trying? I wouldn’t want to try to implement myself the TasksChain algorithm (again!), as it would be error prone and would lead to code duplication. Maybe I could just define an abstract Chain and then implement both TasksChain and StatesChain from there? Or maybe implementing a Chain<T> class?

How would you approach this situation?

The Details:
I have defined an ITask interface:

public interface ITask {
    bool Run();
    ITask FailureTask { get; }
}

and a IState interface that inherits from ITask:

public interface IState : ITask {
    IState FailureState { get; }
}

I have also defined an IHasTasksList interface:

interface IHasTasksList {
    List<Tasks> Tasks { get; }
}

and an IHasStatesList:

interface IHasTasksList {
    List<Tasks> States { get; }
}

Now, I have defined a TasksChain, that is a class that has some code logic that will manipulate a chain of tasks (beware that TasksChain is itself a kind of ITask!):

class TasksChain : ITask, IHasTasksList {
    IList<ITask> tasks = new List<ITask>();

    ...

    public List<ITask> Tasks { get { return _tasks; } }

    ...
}

I am implementing a State the following way:

public class State : IState {
    private readonly TaskChain _taskChain = new TaskChain();

    public State(Precondition precondition, Execution execution) {
        _taskChain.Tasks.Add(precondition);
        _taskChain.Tasks.Add(execution);
    }

    public bool Run() {
        return _taskChain.Run();
    }

    public IState FailureState {
        get { return (IState)_taskChain.Tasks[0].FailureTask; }
    }

    ITask ITask.FailureTask {
        get { return FailureState; }
    }
}

which, as you can see, makes use of explicit interface implementations to “hide” FailureTask and instead show FailureState property.

The problem comes from the fact that I also want to define a StatesChain, that inherits both from IState and IHasStateList (and that also imples ITask and IHasTaskList, implemented as explicit interfaces) and I want it to also hide IHasTaskList‘s Tasks and only show IHasStateList‘s States. (What is contained in “The problem” section should really be after this, but I thought puting it first would be way more reader friendly).

(pff..long text) Thanks!

  • 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-14T14:30:25+00:00Added an answer on May 14, 2026 at 2:30 pm

    On the line where you get an error, you’re trying to return IList<IStates> as if it was an instance of type IList<ITask>. This doesn’t work automtaically, because the two types are different (no matter that the generic parameters are related).

    In C# 3.0 or older, there is no way to achieve that automatically. C# 4.0 adds support for covariance and contravariance, which serves exactly this purpose. But as you noted, this works only when the returned collection is read-only. The IList<T> type doesn’t guarantee that, so it isn’t annotated as covariant in .NET 4.0.

    To make this work using C# 4.0, you’ll need to use truly read-only type, which has covariant annotation in the framework – the best option in your case is IEnumerable<T> (although you could define your own using the out T modifier).

    To add more details, in C# 4.0, you can declare an interface as either covariant or contra-variant. The first case means that the compiler will allow you to perform the conversion you needed in your example (the other case is useful for write-only classes). This is done by adding explicit annotations to the interface declaration (these are already available for .NET 4.0 types). For example, the declaration of IEnumerable<T> has the out annotation meaning that it supports covariance:

    public interface IEnumerable<out T> : IEnumerable { /* ... */ }
    

    Now, the compiler will allow you to write:

    IEnumerable<IState> states = ...
    IEnumerable<ITask> tasks = states;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

@Entity @Inheritance(strategy = InheritanceType.SINGLE_TABLE) public class Problem { @ManyToOne private Person person; } @Entity
The problem: I have a class which contains a template method execute which calls
My problem is that in my Widget class i've the following declaration: MouseEvent* X;
I have a problem applying the DebuggerDisplay attribute on a generic class: [DebuggerDisplay(--foo--)] class
My problem is thus: I need a way to ensure only one given class
I've got a little problem with Zend Framework Zend_Pdf class. Multibyte characters are stripped
I have the following problem using template instantiation [*]. file foo.h class Foo {
I have a class that i want to push_back into a deque. The problem
Expanding upon my earlier problem , I've decided to (de)serialize my config file class
I've got a philosophical programming problem. Let's say I have a class named Employees.

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.