英文:
Is there an interface that can be used as a parameter in a method for T[] (an array of type T) and for List<T>
问题
我有两种执行相同任务的方法:一个接收一个数组参数,另一个接收一个List参数,两者都是字符串类型的。有没有办法用一个单一的方法替代这两个方法?可以用什么类型的参数替代它们?
方法如下:
public static void NumberLinesInCollection(List<string> list, int startNumberingFromRowNumber = 0)
{
int numberOfLines = list.Count;
for (int i = startNumberingFromRowNumber; i < numberOfLines; i++)
{
string sourceString = (i + 1).ToString();
string resultingString = StringOperations.PadWithBlanks(originalString: (i + 1).ToString(),
fieldLength: numberOfLines.ToString().Length,
position: PaddingDirection.left);
list[i] = resultingString + ". " + list[i];
}
}
public static void NumberLinesInCollection(string[] arrayOfStrings, int startNumberingFromRowNumber = 0)
{
int numberOfLines = arrayOfStrings.Length;
for (int i = startNumberingFromRowNumber; i < numberOfLines; i++)
{
string resultingString = StringOperations.PadWithBlanks(originalString: (i + 1).ToString(),
fieldLength: numberOfLines.ToString().Length,
position: PaddingDirection.left);
arrayOfStrings[i] = resultingString + ". " + arrayOfStrings[i];
}
}
谢谢您提前的帮助。
英文:
I have two methods that perform the same task: one receiving an array parameter and the a List parameter, both of type string.
Is there a way to replace these two methods by a single method? What type of parameter can replace both?
The methods are:
public static void NumberLinesInCollection(List<string> list, int startNumberingFromRowNumber = 0)
{
int numberOfLines = list.Count;
for (int i = startNumberingFromRowNumber; i < numberOfLines; i++)
{
string sourceString = (i + 1).ToString();
string resultingString = StringOperations.PadWithBlanks(originalString: (i + 1).ToString(),
fieldLength: numberOfLines.ToString().Length,
position: PaddingDirection.left);
list[i] = resultingString + ". " + list[i];
}
}
and
public static void NumberLinesInCollection(string[] arrayOfStrings, int startNumberingFromRowNumber = 0)
{
int numberOfLines = arrayOfStrings.Length;
for (int i = startNumberingFromRowNumber; i < numberOfLines; i++)
{
string resultingString = StringOperations.PadWithBlanks(originalString: (i + 1).ToString(),
fieldLength: numberOfLines.ToString().Length,
position: PaddingDirection.left);
arrayOfStrings[i] = resultingString + ". " + arrayOfStrings[i];
}
}
Thank you in advance.
答案1
得分: 4
从System.Array的文档中可以看到:
单维数组实现了
IList<T>
,ICollection<T>
,IEnumerable<T>
,IReadOnlyList<T>
和IReadOnlyCollection<T>
通用接口。这些实现在运行时提供给数组,因此在Array类的声明语法中不会出现通用接口。
从List<T>
的文档中可以看到:
实现了
ICollection<T>
,IEnumerable<T>
,IList<T>
,IReadOnlyCollection<T>
,IReadOnlyList<T>
,[以及一些非通用接口]。
正如我们所见,这里存在一个很好的重叠,允许您根据实际需要的数组/列表特性选择最合适的接口。
在您的特定情况下,IList<T>
似乎是最适合的,因为它提供了您的代码中使用的以下两个功能:
- 一个
Item[]
索引器 和 - 一个从
ICollection<T>
继承而来的Count
属性。
英文:
From the documentation of System.Array:
> Single-dimensional arrays implement the IList<T>
, ICollection<T>
, IEnumerable<T>
, IReadOnlyList<T>
and IReadOnlyCollection<T>
generic interfaces. The implementations are provided to arrays at run time, and as a result, the generic interfaces do not appear in the declaration syntax for the Array class.
From the documentation of List<T>
:
> Implements ICollection<T>
, IEnumerable<T>
, IList<T>
, IReadOnlyCollection<T>
, IReadOnlyList<T>
, [and a few non-generic interfaces]
As we can see, there is a nice overlap, allowing you to choose the most fitting interface based on the actual array/list features you require.
In your particular case, IList<T>
seems to be most suitable, since it provides both of the following features used in your code:
- an
Item[]
indexer and - a
Count
property (inherited fromICollection<T>
).
答案2
得分: 1
你可以使用IList<string>
,这将允许您在调用NumberLinesInCollection
时将List和Array都作为参数传递。数组和列表都实现了IList
接口。
英文:
you can use IList<string>
, this will allow you to pass both List and Array as the parameter while calling the NumberLinesInCollection
. array and list both implement IList
答案3
得分: 1
你可以使用 IEnumerable
,然后它可以是 list
和 array
:
public static void NumberLinesInCollection(IEnumerable<string> list, int startNumberingFromRowNumber = 0)
{
}
英文:
You can use IEnumerable
, then it can be either list
and array
:
public static void NumberLinesInCollection(IEnumerable<string> list, int startNumberingFromRowNumber = 0)
{
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论