Why are there variables of interface type, in c#

兰树豪 261 Reputation points

User's image

private ICollection<Resistor_Info> _resistorlist;

“ICollection” Can be a type?

.NET Standard
.NET Standard
A formal specification of .NET APIs that are available on multiple .NET implementations.
494 questions
Microsoft Technologies based on the .NET software framework.
911 questions
Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,294 questions
Visual Studio
Visual Studio
A family of Microsoft suites of integrated development tools for building applications for Windows, the web and mobile devices.
2,824 questions
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
7,630 questions
{count} votes

Accepted answer
  1. Hui Liu-MSFT 16,116 Reputation points Microsoft Vendor

    Hi,@兰树豪. Welcome Microsoft Q&A.

    Yes, "ICollection" is a type. You could use an interface declaration and instantiate it with a class that implements the interface.

    The ICollection<T> interface is the base interface for classes in the System.Collections.Generic namespace.

    The ICollection<T> interface extends IEnumerable<T> and is extended by IDictionary<TKey, TValue> and IList<T>.

    There are two rules I follow:

    Accept the most basic type that will work

    Return the richest type your user will need

    So when writing a function or method that takes a collection, write it not to take a List, but an IList<T>, an ICollection<T>, or IEnumerable<T>. The generic interfaces will still work even for heterogenous lists because System.Object can be a T too. Doing this will save you headache if you decide to use a Stack or some other data structure further down the road. If all you need to do in the function is foreach through it, IEnumerable<T> is really all you should be asking for.

    On the other hand, when returning an object out of a function, you want to give the user the richest possible set of operations without them having to cast around. So in that case, if it's a List<T> internally, return a copy as a List<T>.

    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.
    No comments

1 additional answer

Sort by: Most helpful
  1. Bruce (SqlWork.com) 31,816 Reputation points

    Interfaces are C#'s solution to multiple inheritance, are key to duck typing and the interface design patterns (that is, use interfaces rather than class inheritance). also value types can not inherit, but they can implement interfaces.

    as interfaces define a set of methods and properties and are a type, you can define a variable of that type. you can set the variable to any class instance that implements the interface.