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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T10:22:03+00:00 2026-05-27T10:22:03+00:00

I need some advice/help on this, I can’t see the wood from the trees

  • 0

I need some advice/help on this, I can’t see the wood from the trees any more.

It’s a straight forward series of classes implementing some interfaces using generics.

Then I’m trying to cast the concrete types for example:

MyGenericObject<SomeObject> _obj;

IMyGenericObject<ISomeObject> _genObj = (IMyGenericObject<ISomeObject>)_obj;

// Invalid cast

I’ve read some articles about covariance and contravariance but not too clear why this wouldn’t be possible, or how to get round it?


So, in this example:

public interface IMyObject<in T> where T : IBaseObject
{
    T Activity { get; set; }
}

wouldn’t work…


….because, you can’t get and set the Activity property.

In this example, I needed to do:

public interface IMyObject<out T> where T : IBaseObject
    {
        T Activity { get; }
    }

hope that helps someone, and thanks to all for help!

  • 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-27T10:22:03+00:00Added an answer on May 27, 2026 at 10:22 am

    You can only do that if you declare the interface as having a covariant (out) parameter. You can only do that if the parameter is used covariantly.

    For example, if the interface IMyGenericObject<T> has a method taking a T parameter, this prevents you from declaring the parameter as covariant. Conversely, if there is a method that returns a T, that prevents you from declaring the parameter as contravariant.

    EDIT

    In response to your comment on SLaks’s answer, I’m tempted to repeat everything Eric Lippert has ever written on co- and contravariance. See http://blogs.msdn.com/b/ericlippert/archive/tags/Covariance+and+Contravariance/ and also his answers in SO (most recently https://stackoverflow.com/a/8380213/385844)

    To summarize:

    You can’t cast IList<string> to IList<object> because it’s legal to pass a FileInfo to an IList<object>, but it is not legal to pass it to an IList<string>.

    You can’t cast an IList<object> to an IList<string>, because it’s legal to retrieve an item from an IList<string> and assign it to a string reference, but an IList<object> might contain a FileInfo, which can’t be assigned to a string reference.

    EDIT 2

    Since you asked for advice, it’s also possible to split your interfaces into co- and contravariant parts. To continue with the list example, you could have these interfaces

    public interface ICovariantList<out T>
    {
        T this[int index] { get; }
        //...
    }
    
    public interface IContravariantList<in T>
    {
        T this[int index] { set; }
        void Add(T item);
        //...
    }
    
    public class SomeList<T> : ICovariantList<T>, IContravariantList<T>
    {
        //...
    }
    

    This allows you to use the class covariantly or contravariantly, depending on the context.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need some advice here, I hope somebody can help me. I have the
Need some advice into: Can NSFetchedResultController do this? UITableView: [section name] <= {entity: Section,
I need some advice from experts :) I will develop a website using PHP
I need some help wrapping my head around this. I am trying to develop
I need some advice regarding the use of a command line utility from a
I need some help understanding concepts as Im not sure how to achieve this.
I need some advice on how to successfully update mutiple rows in my database
I need some advice. I’m building an app that has a sequence of 4
I need some advice regarding which database I should use. I want to create
I need some advice of how to setup my tables I currently have a

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.