

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What is the difference between List and IList in C#?
The main difference between List and IList in C# is that List is a class that represents a list of objects which can be accessed by index while IList is an interface that represents a collection of objects which can be accessed by index. The IList interface implemented from two interfaces and they are ICollection and IEnumerable.
List and IList are used to denote a set of objects. They can store objects of integers, strings, etc. There are methods to insert, remove elements, search and sort elements of a List or IList. The major difference between List and IList is that List is a concrete class and IList is an interface. Overall, List is a concrete type that implements the IList interface.
Example 1
using System; using System.Collections.Generic; namespace DemoApplication{ class Demo{ static void Main(string[] args){ IList<string> ilist = new IList<string>(); //This will throw error as we cannot create instance for an IList as it is an interface. ilist.Add("Mark"); ilist.Add("John"); foreach (string list in ilist){ Console.WriteLine(list); } } } }
Example 2
using System; using System.Collections.Generic; namespace DemoApplication{ class Demo{ static void Main(string[] args){ IList<string> ilist = new List<string>(); ilist.Add("Mark"); ilist.Add("John"); List<string> list = new List<string>(); ilist.Add("Mark"); ilist.Add("John"); foreach (string lst in ilist){ Console.WriteLine(lst); } foreach (string lst in list){ Console.WriteLine(lst); } Console.ReadLine(); } } }
Output
The output of the above code is
Mark John Mark John
- Related Questions & Answers
- What is the difference between list and dictionary in C#?
- What is the difference between a python list and a tuple?
- What is the difference between a python list and an array?
- What is the difference between a list and an array in C#?
- What is the difference between == and === in JavaScript?
- What is the difference between $ and @ in R?
- What is the difference between = and: = assignment operators?
- What is the difference between g++ and gcc?
- What is the difference between Hinduism and Buddhism?
- What is the difference between IAS and IPS?
- What is the difference between hastily and quickly?
- What is the difference between Osteoporosis and Osteoarthritis?
- What is the difference between deforestation and afforestation?
- What is the difference between home and house?
- What is the difference between CV and Resume?
Advertisements