

Why refactor the argument of List & lt; Term & gt; to IEnumerable &...
source link: https://www.codesd.com/item/why-refactor-the-argument-of-list-term-to-ienumerable-term.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Why refactor the argument of List & lt; Term & gt; to IEnumerable & lt; Term & gt;
I have a method that looks like this:
public void UpdateTermInfo(List<Term> termInfoList)
{
foreach (Term termInfo in termInfoList)
{
UpdateTermInfo(termInfo);
}
m_xdoc.Save(FileName.FullName);
}
Resharper advises me to change the method signature to IEnumerable<Term>
instead of List<Term>
. What is the benefit of doing this?
The other answers point out that by choosing a "larger" type you permit a broader set of callers to call you. Which is a good enough reason in itself to make this change. However, there are other reasons. I would recommend that you make this change because when I see a method that takes a list or an array, the first thing I think is "what if that method tries to change an item in my list/array?"
You want the contents of a bucket, but you are requiring not just the bucket but also the ability to change its contents. Why would you require that if you're not going to use that ability? When you say "this method cannot take any old sequence; it has to take a mutable list that is indexed by integers" I think that you're making that requirement on the caller because you're going to take advantage of that power.
If "I'm planning on messing up your data structure" is not what you intend to communicate to the caller of the method then don't communicate that. A method that takes a sequence communicates "The most I'm going to do is read from this sequence in order".
Recommend
-
10
Linq to Collections: Beyond IEnumerable<T> posted by Craig Gidney on December 18, 2012 The latest version of the .Net...
-
16
IEnumerator、IEnumerable这两个接口单词相近、含义相关,傻傻分不清楚。 入行多年,一直没有系统性梳理这对李逵李鬼。 最近本人在怼着why神的《其实吧,LRU也就那么回事》,方案1使用数组实现LRU,手写算法涉及这一对接口,借...
-
4
Counting a Non-Generic IEnumerable You rarely come across them in 2021, but there is such a thing as a non-generic IEnumerable. They exist in System.Collections rather than System.Collections.Generic
-
5
IEnumerable<T> Interface Definition Namespace: System.Collections.Generic Assembly:System.Runti...
-
4
.NET Collections – IEnumerable, IQueryable, ICollection Posted by Code Maze | Updated Date Nov 1, 2021 |
-
2
André Slupik Posted on Mar 3...
-
8
View .NET collections with the new IEnumerable Debugger Visualizer Harshada H April 21st, 2022 While debugging .NET code,...
-
8
IEnumerable Visualizer In Visual Studio 2022Visual Studio 2022 17.2 shipped the other day, and in it was a handy little feature that I can definitely see myself using a lot going forward. That is the IEnumerable Visu...
-
9
-
8
IEnumerable, ICollection, IList and List
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK