I have been learning about collections such as arrays and arraylists and lists and have come across the term "interface" a fair bit. I know that an IList is an "interface" but I dont understand what an interface is or does.

  1. What is an interface in C#?
  2. How does IList differ from List?
  3. Where do we use an interface?
  4. When do we use an interface in our code?

This question is probably a simple one but I am still learning, Thankyou

Recommended Answers

All 3 Replies

An interface defines what you can do with a certain class. The interface only defines what methods should be supported. The actual implementation lies within the class. Think of it as a blueprint. It doesn't do anything itself, it just ensures something is built in a certain way.

What is an interface in C#?

Click Me.
And me too.

How does IList differ from List?

List is an implementation of IList.

Where do we use an interface?

That's a harder question. The most basic answer is that you use an interface when you want to support potentially many implementations of functionality that support the same methods, properties, indexers, and events. IEnumerable is a good example in that you can return a number of collections as IEnumerable and the caller doesn't need to know or care what the actual collection is as long as they follow the interface provided by IEnumerable.

When do we use an interface in our code?

That's more of a style question. You'll see recommendations across the board from avoiding interfaces because they complicate the design to using interfaces wherever possible to enable scalability and flexibility on the back-end should you choose to change implementations without changing how callers use a class.

Try to think of an interface as a class of classes. Each seperate class could be of a different type. It allows you to create very complex models.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.