Why are there variables of interface type, in c#

兰树豪 381 Reputation points
2023-03-14T08:09:09.1966667+00:00

User's image

private ICollection<Resistor_Info> _resistorlist;

“ICollection” Can be a type?

.NET
.NET
Microsoft Technologies based on the .NET software framework.
4,103 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,853 questions
Visual Studio
Visual Studio
A family of Microsoft suites of integrated development tools for building applications for Windows, the web and mobile devices.
5,449 questions
C#
C#
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.
11,475 questions
{count} votes

Accepted answer
  1. Hui Liu-MSFT 48,666 Reputation points Microsoft External Staff
    2023-03-14T09:18:07.4433333+00:00

    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.

    2 people found this answer helpful.
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Bruce (SqlWork.com) 75,871 Reputation points Moderator
    2023-03-14T20:35:01.9866667+00:00

    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.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.