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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T16:34:53+00:00 2026-05-11T16:34:53+00:00

interface IFolderOrItem<TFolderOrItem> where TFolderOrItem : FolderOrItem {} abstract class FolderOrItem {} class Folder :

  • 0
interface IFolderOrItem<TFolderOrItem> where TFolderOrItem : FolderOrItem {}

abstract class FolderOrItem {}

class Folder : FolderOrItem {}

abstract class Item : FolderOrItem {}

class Document : Item {}

now i’m trying to do sth like this:

class Something
{
    IFolderItemOrItem<Item> SelectedItem { get; set; }
    void SomeMagicMethod()
    {
        this.SelectedItem = (IFolderOrItem<Item>)GetMagicDocument();
        // bad bad bad ... ??
    }
    IFolderOrItem<Document> GetMagicDocument()
    {
        return someMagicDocument; // which is of type IFolderOrItem<Document>
    }
}

is there any possibility to get this working?

  • 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-11T16:34:54+00:00Added an answer on May 11, 2026 at 4:34 pm

    If I read it correctly… then the problem is that just because Foo : Bar, that does not mean that ISomething<Foo> : ISomething<Bar>…

    In some cases, variance in C# 4.0 may be an option. Alternatively, there are sometimes things you can do with generic methods (not sure it will help here, though).


    The closest you can do in C# 3.0 (and below) is probably a non-generic base interface:

    interface IFolderOrItem {}
    interface IFolderOrItem<TFolderOrItem> : IFolderOrItem
        where TFolderOrItem : FolderOrItem { }
    

    commonly, the base-interface would have, for example, a Type ItemType {get;} to indicate the real type under consideration. Then usage:

    IFolderOrItem SelectedItem { get; set; }
    ...
    public void SomeMagicMethod()
    {
        this.SelectedItem = GetMagicDocument(); // no cast needed
        // not **so** bad
    }
    

    From the spec, this relates to §25.5.6 (ECMA 334 v4):

    25.5.6 Conversions

    Constructed types follow the same conversion rules (§13)
    as do non-generic types. When applying
    these rules, the base classes and
    interfaces of constructed types shall
    be determined as described in §25.5.3.

    No special conversions exist between
    constructed reference types other than
    those described in §13. In particular,
    unlike array types, constructed
    reference types do not permit
    co-variant conversions (§19.5). This
    means that a type List<B> has no
    conversion (either implicit or
    explicit) to List<A> even if B is
    derived from A. Likewise, no
    conversion exists from List<B> to
    List<object>.

    [Note: The rationale for
    this is simple: if a conversion to
    List<A> is permitted, then apparently,
    one can store values of type A into
    the list. However, this would break
    the invariant that every object in a
    list of type List<B> is always a value
    of type B, or else unexpected failures
    can occur when assigning into
    collection classes. end note]

    The same applies to interfaces. This changes a bit in C# 4.0, but only in some cases.

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

Sidebar

Ask A Question

Stats

  • Questions 203k
  • Answers 203k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I was going to recommend a ScoreDocComparator, but I see… May 12, 2026 at 8:34 pm
  • Editorial Team
    Editorial Team added an answer You can do this with array_walk, it would be something… May 12, 2026 at 8:34 pm
  • Editorial Team
    Editorial Team added an answer Many ways to do it, here's one way: SELECT LEFT(DATENAME(dw,… May 12, 2026 at 8:34 pm

Related Questions

Interface Builder can be used for basic dependency injection in a Cocoa app, but
Interface IView { List<string> Names {get; set;} } public class Presenter { public List<string>
interface I { int J(); } class A : I { public int J(){return
Interface vs. Base class is still a fairly gray area to me. I'm reading

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.