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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T19:22:48+00:00 2026-05-23T19:22:48+00:00

Consider the code snippet. IList<String> obj=new List<string>(); IEnumerable<Object> obj1 = obj; But if i

  • 0

Consider the code snippet.

IList<String> obj=new List<string>();
IEnumerable<Object> obj1 = obj;

But if i write ICollection<Object> obj2 = obj; it throws me a compile time error.

Cannot implicitly convert type ‘System.Collections.Generic.IList<string>‘ to ‘System.Collections.Generic.ICollection<object>‘.

Why is this behavior since List<T> implements both IEnumerable<T> and ICollection<T> and also IList<T> is defined as

public interface IList<T> : ICollection<T>, IEnumerable<T>, IEnumerable
{
    T this[int index] { get; set; }
    int IndexOf(T item);
    void Insert(int index, T item);
    void RemoveAt(int index);
}
  • 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-23T19:22:50+00:00Added an answer on May 23, 2026 at 7:22 pm

    ICollection<T> is not covariant on the type parameter, whereas IEnumerable<T> is. If you look at their declarations (ICollection, IEnumerable) you can see that IEnumerable<T> uses the out keyword on T, while ICollection<T> does not.

    This makes sense if you think about it, since (roughly speaking) covariance is safe when the interface will only be used to read objects (and thus the out keyword). IEnumerable<T> clearly meets that criterion, whereas ICollection<T> is quite the opposite.

    As an example of what could go wrong (using your example):

    IList<String> obj = new List<string>(); // Legal, of course
    ICollection<Object> obj1 = obj;         // Illegal, but let's see what happens
    obj1.Add(new NonStringObject());        // That's not a string being stored in a List<string>
    

    Remember: covariance is not the same as inheritance. Just because two classes or interfaces share an inheritance relation does not mean their type parameters share the same variance characteristics.

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

Sidebar

Related Questions

Consider the following code snippet private void ProcessFile(string fullPath) { XmlTextReader rdr = new
Consider the following code snippet within a class private static Object _syncroot = new
Consider this snippet of code: public static class MatchCollectionExtensions { public static IEnumerable<T> AsEnumerable<T>(this
Consider this code... using System.Threading; //... Timer someWork = new Timer( delegate(object state) {
Consider this code snippet: class MyClass{ private List myList; //... public List getList(){ return
Consider this code snippet: class Program { static void Main(string[] args) { Console.WriteLine(Test().ToString()); }
Consider the following code snippet: list<someClass>& method(); .... list<someClass> test = method(); What will
Consider the following snippet of code: <div align=Left> <font style='font-size:17pt; font-family:Times New Roman;color:#000000;'> <textformat
Consider this code snippet: public static void main(String[] args) { int z1 = 0;
Consider the following code Snippet Form form2 = new Form(); test(form2); form2.Show(); public void

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.