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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T09:47:58+00:00 2026-05-26T09:47:58+00:00

Currently I’m preparing a presentation of the new generic variance features in C# for

  • 0

Currently I’m preparing a presentation of the new generic variance features in C# for my colleagues. To cut the story short I wrote following lines:

IList<Form> formsList = new List<Form> { new Form(), new Form() };
IList<Control> controlsList = formsList;

Yes, this is of course not possible, as IList(Of T) is invariant (at least my thought). The compiler tells me that:

Cannot implicitly convert type
System.Collections.Generic.IList<System.Windows.Forms.Form> to
System.Collections.Generic.IList<System.Windows.Forms.Control>. An
explicit conversion exists (are you missing a cast?)

Hm, does this mean I can force an explicit conversion? I just tried it:

IList<Form> formsList = new List<Form> { new Form(), new Form() };
IList<Control> controlsList = (IList<Control>)formsList;

And… it compiles! Does it mean I can cast the invariance away? – At least the compiler is ok with that, but I just turned the former compile time error to a run time error:

Unable to cast object of type 'System.Collections.Generic.List`1[System.Windows.Forms.Form]' to type 'System.Collections.Generic.IList`1[System.Windows.Forms.Control]'.

My question(s): Why can I cast the invariance of IList<T> (or any other invariant interface as to my experiments) away? Do I really cast the invariance away, or what kind of conversion happens here (as IList(Of Form) and IList(Of Control) are completely unrelated)? Is this a dark corner of C# I didn’t know?

  • 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-26T09:47:59+00:00Added an answer on May 26, 2026 at 9:47 am

    Essentially, a type could implement IList<Control> as well as IList<Form> so it’s possible for the cast to succeed – so the compiler lets it through for the time being (aside: it could potentially be smarter here and produce a warning because it knows the concrete type of the referenced object, but doesn’t. I don’t think it would be appropriate to produce a compiler-error since it is not a breaking change for a type to implement a new interface).

    As an example of such a type:

    public class EvilList : IList<Form>, IList<Control> { ... }

    What happens at run-time is just a CLR type-check. The exception you are seeing is representative of a failure of this operation.

    The IL generated for the cast is:

    castclass [mscorlib]System.Collections.Generic.IList`1<class [System.Windows.Forms]System.Windows.Forms.Control>
    

    From MSDN:

    The castclass instruction attempts to cast the object reference (type
    O) atop the stack to a specified class. The new class is specified by
    a metadata token indicating the desired class. If the class of the
    object on the top of the stack does not implement the new class
    (assuming the new class is an interface) and is not a derived class of
    the new class then an InvalidCastException is thrown. If the object
    reference is a null reference, castclass succeeds and returns the new
    object as a null reference.

    InvalidCastException is thrown if obj cannot be cast to class.

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

Sidebar

Related Questions

Currently have the following expression: @[TMS_Dest] + \\ + @[TMS_Rename] + (DT_WSTR,4)DatePart(yyyy, GetDate()) +
Currently I have the following Mapping in my Controller: @RequestMapping( value = /add.html, method
I want use html5's new tag to play a wav file (currently only supported
Currently I insert a new relationship by everytime checking, if it doesn't exist: unless
Currently I have a database with the following relationships: One Client has multiple Entities
Currently I am filtering incoming mails by a .forward in the following way: if
Currently, I don't really have a good method of debugging JavaScript in Internet Explorer and
Currently my workflow with Emacs when I am coding in C or C++ involves
Currently we have a hybrid ASP/PHP setup connecting to a SQL Server 2005 database.
Currently we have a project with a standard subversion repository layout of: ./trunk ./branches

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.