Difference between IList<T> and IEnumerable<T>?

Before going into the deep let's consider a simple example below

IList<string> strings = new List<string>{"a","b","c"};

IList<object> objects = strings; (invalid No conversion from IList<string> to IList<object>)

Now the question is why it is not allowed and the next question is what is the difference between IList<T> and iEnumerable<T>?

if i will answer this question in simple word , it would not be safe .

Because the methods within IList<T> allows values of type T as input as well as output. What ever way you use IEnumerable<T> ends up with T value as a output, But there are few methods in IList<T> like Add takes T values as an Input.

Let's understand it in more comprehensive way, take one example

IList<string> strings = new List<string>{"a","b","c"};

IList<object> objects = strings;

objects.Add(new object());

string element = objects[3]; (Retrieves it as a string)









To view or add a comment, sign in

More articles by Sayed Azharuddin

  • Understanding Task and ValueTask in C#

    Let’s dig in. What is Task<T>? Let's understand the basics of Task then will go in details .

  • Thinking Asynchrony in C#

    If we will ask any Developer about asynchronous execution , there might be chances that he/she will talk about the…

  • Variance in C#

    Before going deep let's recall one principle from SOLID that is Liskov Substitution Principle , it states that even if…

  • Dynamic Typing in c#

    Consider the below example for better understanding a corner case of Dyamic typing. List<Int> list1= new List<Int>();…

  • 12 ways strategic silence can make you more powerful

    Use silence to your advantage. Silence is often perceived as awkward and uncomfortable, but it doesn't have to be.

  • How not to screw up your new job and derail your career

    The strategies are pretty straightforward. Focus on learning Repress the urge to do something just to prove yourself…

  • Beat your fear of public speaking with these 7 science-backed strategies

    (Paul Kane/Getty) Virgin Group founder Richard Branson says he hates public speaking — but he manages to do it anyway…

  • 3 Keys to making a presentation that will impress your boss

    There were three common responses: 1. Be clear.

  • What Is .NET

    Being a contributor to the Code Project for quite some time now, it is commendable to see so many articles from various…

Explore content categories