
- C# Basic Tutorial
- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Decision Making
- C# - Loops
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Multithreading
- C# Useful Resources
- C# - Questions and Answers
- C# - Quick Guide
- C# - Useful Resources
- C# - Discussion
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 Articles
- What is the difference between list and dictionary in C#?
- What is the difference between a list and an array 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 does the interface IList do in C#?
- What is the difference between working of append and + operator in a list in Python?
- What is the difference between $ and @ in R?
- What is the difference between == and === in JavaScript?
- Difference between the list() and listFiles() methods in Java
- Difference between List and Set in Java
- Difference between List and Tuples in Python.
- Difference Between List and Tuple in Python
- Difference Between List and ArrayList in Java
- What is the difference between /* */ and /** */ comments in Java?
- What is the difference between NA and in R?

Advertisements